<?php
/**
* @module		Art Sexy Lightbox
* @copyright	Copyright (C) 2009 artetics.com
* @license		GPL
*/

defined("_JEXEC") or die("Restricted access");

jimport("joomla.plugin.plugin");
error_reporting(E_ERROR);

if (file_exists(JPATH_SITE . DS . 'plugins' . DS . 'content' . DS . 'artsexylightbox' . DS . 'library' . DS . 'json.php')) {
	require_once(JPATH_SITE . DS . 'plugins' . DS . 'content' . DS . 'artsexylightbox' . DS . 'library' . DS . 'json.php');
} else if (file_exists(JPATH_SITE . DS . 'plugins' . DS . 'content' . DS . 'artsexylightbox' . DS . 'artsexylightbox' . DS . 'library' . DS . 'json.php')) {
	require_once(JPATH_SITE . DS . 'plugins' . DS . 'content' . DS . 'artsexylightbox' . DS . 'artsexylightbox' . DS . 'library' . DS . 'json.php');
}

if (file_exists(JPATH_SITE . DS . 'plugins' . DS . 'content' . DS . 'artsexylightbox' . DS . 'library' . DS . 'asido' . DS . 'class.asido.php')) {
	require_once(JPATH_SITE . DS . 'plugins' . DS . 'content' . DS . 'artsexylightbox' . DS . 'library' . DS . 'asido' . DS . 'class.asido.php');
} else if (file_exists(JPATH_SITE . DS . 'plugins' . DS . 'content' . DS . 'artsexylightbox' . DS . 'artsexylightbox' . DS . 'library' . DS . 'asido' . DS . 'class.asido.php')){
	require_once(JPATH_SITE . DS . 'plugins' . DS . 'content' . DS . 'artsexylightbox' . DS . 'artsexylightbox' . DS . 'library' . DS . 'asido' . DS . 'class.asido.php');
}


class plgContentartsexylightbox extends JPlugin {
	function plgContentartsexylightbox(&$subject, $params) {
		parent::__construct($subject, $params);
	}

	function onContentPrepare($context, $article, $params, $page = 0) { 
		plgContentartsexylightbox::onPrepareContent($article, $params, 0);
	}
	function onPrepareContent($article, $params, $limitstart) {
		$regexp = '/\{artsexylightbox((\s+[a-z\_0-9]+=(?:"[^"]*"|&quot;.*?&quot;|[^\s}]*))*\s*)\}(?:(.*?){\/artsexylightbox\})?/si';
		if($params && $params->get('enabled', '1') == "0") {
			$article->text = preg_replace($regex, '', $article->text);
		} else {
			if (file_exists(JPATH_SITE . DS . 'plugins' . DS . 'content' . DS . 'artsexylightbox' . DS . 'library' . DS . 'asido' . DS . 'class.asido.php')) {
				$artParams = array('path' => '',
							 'name' => 'SLB',
							 'zIndex' => 65555,
							 'color' => 'black',
							 'find' => 'sexylightbox',
							 'imagesdir' => JURI::BASE() . 'plugins/content/artsexylightbox/images',
							 'background' => 'bgSexy.png',
							 'backgroundIE' => 'bgSexy.gif',
							 'closeButton' => 'SexyClose.png',
							 'displayed' => 0,
							 'modal' => 0,
							 'showDuration' => 200,
							 'showEffect' => 'linear',
							 'closeDuration' => 400,
							 'closeEffect' => 'linear',
							 'moveDuration' => 800,
							 'resizeDuration' => 800,
							 'moveEffect' => 'easeOutBack',
							 'resizeEffect' => 'easeOutBack',
							 'noConflict' => false,
							 'previewWidth' => '',
							 'previewHeight' => '',
				 'loadJQuery' => 1);
			} else {
				$artParams = array('path' => '',
							 'name' => 'SLB',
							 'zIndex' => 65555,
							 'color' => 'black',
							 'find' => 'sexylightbox',
							 'imagesdir' => JURI::BASE() . 'plugins/content/artsexylightbox/artsexylightbox/images',
							 'background' => 'bgSexy.png',
							 'backgroundIE' => 'bgSexy.gif',
							 'closeButton' => 'SexyClose.png',
							 'displayed' => 0,
							 'modal' => 0,
							 'showDuration' => 200,
							 'showEffect' => 'linear',
							 'closeDuration' => 400,
							 'closeEffect' => 'linear',
							 'moveDuration' => 800,
							 'resizeDuration' => 800,
							 'moveEffect' => 'easeOutBack',
							 'resizeEffect' => 'easeOutBack',
							 'noConflict' => false,
							 'previewWidth' => '',
							 'previewHeight' => '',
				 'loadJQuery' => 1);
			}
			
			if ($this && $this->params && isset($this->params->_registry) && $this->params->_registry['_default'] && count($this->params->_registry['_default']['data'])) {
				$pluginParams = plgContentartsexylightbox::getPluginParams($artParams, $this->params->_registry['_default']['data']);
			} else {
				$pluginParams = plgContentartsexylightbox::getPluginParams($artParams);
			}
			
			if (preg_match_all($regexp, $article->text, $matches, PREG_SET_ORDER) > 0) {
				plgContentartsexylightbox::loadExtensions();
				$i = 0;
				$matchesCount = count($matches);
				foreach ($matches as $match) {
					$adjustedMatch = plgContentartsexylightbox::adjustMatch($match[1]);
					$adjustedMatch = parse_str($adjustedMatch, $clientParams);
					$finalParams = plgContentartsexylightbox::getClientParams($clientParams, $pluginParams);
					$article->text = preg_replace($regexp, plgContentartsexylightbox::getSexyLightboxCode($i, $finalParams, $match[3]), $article->text, 1);
					$i++;
					$finalParams = $pluginParams;
				}
			}
		}
	}
	
	function adjustMatch($text) {
		$adjustedMatch = ltrim($text);
		$adjustedMatch = rtrim($adjustedMatch);
		$adjustedMatch = str_replace('&', '|||', $adjustedMatch);
		$adjustedMatch = str_replace(' =', '=', $adjustedMatch);
		$adjustedMatch = str_replace('= ', '=', $adjustedMatch);
		$adjustedMatch = str_replace('="', '=', $adjustedMatch);
		$adjustedMatch = str_replace('= ', '=', $adjustedMatch);
		$adjustedMatch = str_replace('" ', '"', $adjustedMatch);
		$adjustedMatch = str_replace(' "', '"', $adjustedMatch);
		$adjustedMatch = str_replace('"', '&', $adjustedMatch);
		$adjustedMatch = str_replace(' ', '+', $adjustedMatch);
		$adjustedMatch = rtrim($adjustedMatch, '&');
		return $adjustedMatch;
	}
	
	function getSexyLightboxCode($id, $finalParams, $content = '') {
		/** Parameters **/
		
		/** Color **/
		$color = $finalParams['color'];
		if (!$color) {
			$color = 'oldblack';
		}
		
		/** Dimensions **/
		$previewWidth = $finalParams['previewWidth'];
		$previewHeight = $finalParams['previewHeight'];
		$width = $finalParams['width'];
		$height = $finalParams['height'];
		
		/** Flickr parameters**/
		$flickr = $finalParams['flickr'];
		$flickrImage = $finalParams['flickrImage'];
		$flickrNumberOfImages = $finalParams['flickrNumberOfImages'];
		if (!$flickrNumberOfImages) {
			$flickrNumberOfImages = 20;
		}
		$page = $finalParams['page'];
		if (!$page) {
			$page = 1;
		}
		$flickrApiKey = $finalParams['flickrApiKey'];
		$flickrSet = $finalParams['flickrSet'];
		$flickrThumbSize = $finalParams['flickrThumbSize'];
		if (!isset ($flickrThumbSize)) {	
			$flickrThumbSize = "s";
		}
		$flickrImageSize = $finalParams['flickrImageSize'];
		if (!isset ($flickrImageSize)) {
			$flickrImageSize = "m";
			if (isset($flickrApiKey)) {
				$flickrImageSize = "";
			}
		}
		$flickrLargeImages = $finalParams['flickrLargeImages'];
		$flickrSmallThumbs = $finalParams['flickrSmallThumbs'];
		
		/** Picasa parameters**/
		$picasaUser = $finalParams['picasaUser'];
		$finalParams['picasaUser'] = '';
		$picasaAlbum = $finalParams['picasaAlbum'];
		$picasaImageSize = $finalParams['picasaImageSize'];
		$picasaLargeImageSize = $finalParams['picasaLargeImageSize'];
		if (!$picasaLargeImageSize) {
		  $picasaLargeImageSize = 1280;
		}
		
		/** Photobucket parameters **/
		$photobucket = $finalParams['photobucket'];
		
		/** Path **/
		$path = $finalParams['path'];
		$pattern = $finalParams['pattern'];
		$subFolders = $finalParams['subFolders'];
		$thumbnailPath = $finalParams['thumbnailPath'];
		$largeImagePath = $finalParams['largeImagePath'];
		$remoteDirListing = $finalParams['remoteDirListing'];
    
		$fileList = $finalParams['fileList'];
    
		/** Module **/
		$module = $finalParams['module'];
		
		/** Convert images **/
		$autoGenerateThumbs = $finalParams['autoGenerateThumbs'];
		$convertImage = $finalParams['convertImage'];
		if (!$convertImage) {
			$convertImage = 'resize';
		}
		$cropStart = $finalParams['cropStart'];
		
		/** Article parameters **/
		$section = $finalParams['section'];
		$category = $finalParams['category'];
		$articleContentOnly = $finalParams['articleContentOnly'];
		
		/** Carousel parameters **/
		$carousel = $finalParams['carousel'];
		$carousel_visible = $finalParams['carousel_visible'];
		$auto = $finalParams['auto'];
		$speed = $finalParams['speed'];
		$vertical = $finalParams['vertical'];
		if (!$vertical) {
			$vertical = 'false';
		}
		if (!$carousel_visible) {
			$carousel_visible = 5;
		}
		$carousel_scroll = $finalParams['carousel_scroll'];
		if (!$carousel_scroll) {
			$carousel_scroll = 1;
		}
    
		/** Autocarousel parameters **/
		$autoCarousel = $finalParams['autoCarousel'];
		
		/** Cloud carousel parameters **/
		$cloudCarousel = $finalParams['cloudCarousel'];
		$containerWidth = $finalParams['containerWidth'];
		if (!$containerWidth) {
			$containerWidth = '500px';
		}
		$containerHeight = $finalParams['containerHeight'];
		if (!$containerHeight) {
			$containerHeight = '500px';
		}
		if (!$finalParams['reflHeight']) {
			$finalParams['reflHeight'] = 56;
		}
		if (!$finalParams['reflGap']) {
			$finalParams['reflGap'] = 2;
		}
		if (!$finalParams['yRadius']) {
			$finalParams['yRadius'] = 40;
		}
		if (!$finalParams['xPos']) {
			$finalParams['xPos'] = 285;
		}
		if (!$finalParams['yPos']) {
			$finalParams['yPos'] = 120;
		}
		if (!$finalParams['titleBox']) {
			$finalParams['titleBox'] = '.cloud_carousel_title';
		}
		$hideButtons = $finalParams['hideButtons'];

		/** Inline parameters **/
		$width = $finalParams['width'];
		$height = $finalParams['height'];
		$background = $finalParams['background'];
		$modal = $finalParams['modal'];
		$text = $finalParams['text'];
		$title = $finalParams['title'];
		$hideContent = $finalParams['hideContent'];
		if (!$height) {
			$height = 600;
		}
		if (!$width) {
			$width = 800;
		}
		
		/** Other parameters **/
		$preload = $finalParams['preload'];
		$loadJQuery = $finalParams['loadJQuery'];
		$awsAccessKey = $finalParams['awsAccessKey'];
		$awsSecretKey = $finalParams['awsSecretKey'];
		$awsBucket = $finalParams['awsBucket'];
		$awsFolder = $finalParams['awsFolder'];
		$noConflict = $finalParams['noConflict'];
		$singleImage = $finalParams['singleImage'];
		$showSingleImage = $finalParams['showSingleImage'];
		$image = $finalParams['image'];
		$article = $finalParams['article'];
		$link = $finalParams['link'];
		$thumbImage = $finalParams['thumbImage'];
		if ($finalParams['singleText']) {
			$finalParams['singleText'] = str_replace('[', '&#91;', $finalParams['singleText']);
			$finalParams['singleText'] = str_replace(']', '&#93;', $finalParams['singleText']);
		}
		$singleText = $finalParams['singleText'];
		$thumbnailPreviewCount = $finalParams['thumbnailPreviewCount'];
		$popup = $finalParams['popup'];
		$numberOfImages = $finalParams['numberOfImages'];
		$magnifier = $finalParams['magnifier'];
		$sort = $finalParams['sort'];
		$sortBy = $finalParams['sortBy'];
    if (!$sortBy) {
      $sortBy = 'title';
    }
		$pAlt = $finalParams['alt'];
		if (!$pAlt) {
		  $pAlt = 'image';
		}
		$caption = $finalParams['caption'];
		$lang2 = $finalParams['lang'];
		$defaultWidth = $finalParams['defaultWidth'];
		$defaultHeight = $finalParams['defaultHeight'];
    $autoLoad = $finalParams['autoLoad'];
    $autoClose = $finalParams['autoClose'];
		$articleAutoLoad = $finalParams['articleAutoLoad'];
    
		$watermark = $finalParams['watermark'];
		$watermarkPosition = $finalParams['watermarkPosition'];
		if (!$watermarkPosition) {
		  $watermarkPosition = 'topleft';
		}

		$cycle = $finalParams['cycle'];
		$cycleSpeed = $finalParams['cycleSpeed'];
		$cycleEffect = $finalParams['cycleEffect'];
		if (!$cycleEffect) {
		  $cycleEffect = 'fade';
		}
		
		$showHits = $finalParams['showHits'];
		$contentId = $finalParams['contentId'];
    
		$youtube = $finalParams['youtube'];
		
		if ($defaultWidth && $defaultHeight) {
			$finalParams['BoxStyles'] = '{width: "' . $defaultWidth . '", height: "' . $defaultHeight . '"}';
		}
		
		$id2 = uniqid('container_', false);
		$imagesCode = '<div class="artsexylightbox_container" id="' . $id2 . '"';
		if ($cloudCarousel) {
			$imagesCode .= 'style="width:' . $containerWidth . ';height:' . $containerHeight . '" ';
		}
		$imagesCode .= '>';
		
		/** Add resources **/
		$document = &JFactory::getDocument();
		$j6 = 'artsexylightbox/';
		if (file_exists(JPATH_SITE . DS . 'plugins' . DS . 'content' . DS . 'artsexylightbox' . DS . 'library' . DS . 'asido' . DS . 'class.asido.php')) {
			$j6 = '';
		}
		
		if ($cloudCarousel && $loadJQuery != 'false') {
			$document->addScript(JURI::root() . 'plugins/content/artsexylightbox/' . $j6 . 'js/jquery.1.4.js');
		} else if ($loadJQuery != 'false') {
			$document->addScript(JURI::root() . 'plugins/content/artsexylightbox/' . $j6 . 'js/jquery.js');
		}
		$document->addScript(JURI::root() . 'plugins/content/artsexylightbox/' . $j6 . 'js/jquery.easing.1.3.js');
		if ($carousel) {
			$document->addScript(JURI::root() . 'plugins/content/artsexylightbox/' . $j6 . 'js/jcarousel.js');
		}
		if ($cloudCarousel) {
			$document->addScript(JURI::root() . 'plugins/content/artsexylightbox/' . $j6 . 'js/cloud-carousel.js');
			$document->addStyleSheet(JURI::root() . 'plugins/content/artsexylightbox/' . $j6 . 'css/cloudcarousel.css');
		}
    
		if ($autoCarousel) {
		  $document->addScript(JURI::root() . 'plugins/content/artsexylightbox/' . $j6 . 'js/right.js');
		  $document->addStyleSheet(JURI::root() . 'plugins/content/artsexylightbox/' . $j6 . 'css/right.css');
		}
		
		if ($color == 'white' || $color == 'black') {
			$document->addScript(JURI::root() . 'plugins/content/artsexylightbox/' . $j6 . 'js/sexylightbox.v2.2.jquery.min.js');
			$document->addStyleSheet( JURI::root() . 'plugins/content/artsexylightbox/' . $j6 . 'css/oldsexylightbox.css' );
		} else {
			if ($color == 'spanish_white') {
				$color = 'blanco';
				$finalParams['color'] = 'blanco';
			} else if ($color == 'spanish_black') {
				$color = 'negro';
				$finalParams['color'] = 'negro';
			}
			$document->addScript(JURI::root() . 'plugins/content/artsexylightbox/' . $j6 . 'js/sexylightbox.v2.3.4.jquery.min.js');
			$document->addStyleSheet( JURI::root() . 'plugins/content/artsexylightbox/' . $j6 . '/css/sexylightbox.css' );
		}
		$document->addScript(JURI::root() . 'plugins/content/artsexylightbox/' . $j6 . 'js/jquery.flickr.js');
		if ($cycle) {
		  $document->addScript(JURI::root() . 'plugins/content/artsexylightbox/' . $j6 . 'js/jquery.cycle.all.min.js');
		}
		$document->addScript(JURI::root() . 'plugins/content/artsexylightbox/' . $j6 . 'js/jquery.nc.js');
		
		/** Begin inline javascript code **/
		$jsCode .= '<script type="text/javascript" charset="utf-8">asljQuery(function(){asljQuery(document).ready(function(){';
		if ($cycle) {
		  if ($cycleSpeed) {
			$jsCode .= 'asljQuery("#' . $id2 . '").cycle({fx:"' . $cycleEffect . '", timeout:  ' . $cycle . ' , speed: ' . $cycleSpeed . '});';
		  } else {
			$jsCode .= 'asljQuery("#' . $id2 . '").cycle({fx:"' . $cycleEffect . '", timeout:  ' . $cycle . ' });';
		  }     
		}
		/** ID for gallery **/
		$id = uniqid('artgallery_', false);
		
		if ($cloudCarousel && !$hideButtons) {
			$finalParams['buttonLeft'] = '#carousel_left' . $id;
			$finalParams['buttonRight'] = '#carousel_right' . $id;
		}
		
		/** Encode input parameters **/
		$jsonHandler = new ART_Services_JSON(SERVICES_JSON_LOOSE_TYPE);
		$dFP = !empty($finalParams) ? $jsonHandler->encode($finalParams) : '';
		
    $awsSecretKey = str_replace(' ', '+', $awsSecretKey);
		/** Read images from remote server **/
    if ($awsAccessKey && $awsSecretKey && $awsBucket) {
	  if (file_exists(JPATH_SITE . DS . 'plugins' . DS . 'content' . DS . 'artsexylightbox' . DS . 'library' . DS . 'S3.php')) {
      require_once(JPATH_SITE . DS . 'plugins' . DS . 'content' . DS . 'artsexylightbox' . DS . 'library' . DS . 'S3.php');
	  } else {
      require_once(JPATH_SITE . DS . 'plugins' . DS . 'content' . DS . 'artsexylightbox' . DS . 'artsexylightbox' . DS . 'library' . DS . 'S3.php');
	  }
      S3::setAuth($awsAccessKey, $awsSecretKey);
	  $finalParams['awsAccessKey'] = '';
	  $finalParams['awsSecretKey'] = '';
	  $finalParams['awsBucket'] = '';
	  $finalParams['awsFolder'] = '';
      if (($contents = S3::getBucket($awsBucket)) !== false) {
        if (!file_exists(JPATH_SITE . DS . 'images' . DS . 'artsexylightbox_tmp')) {
          mkdir (JPATH_SITE . DS . 'images' . DS . 'artsexylightbox_tmp');
        }
        if (!file_exists(JPATH_SITE . DS . 'images' . DS . 'artsexylightbox_tmp' . DS . $awsBucket)) {
          mkdir (JPATH_SITE . DS . 'images' . DS . 'artsexylightbox_tmp' . DS . $awsBucket);
        }
        set_time_limit(3000);
        foreach ($contents as $object) {
			if ($awsFolder) {
				if ($this->isImage($object['name']) && strstr($object['name'], $awsFolder) !== FALSE) {
					$newName = str_replace('/', '_', $object['name']);
					if (!file_exists(JPATH_SITE . DS . 'images' . DS . 'artsexylightbox_tmp' . DS . $awsBucket . DS . $newName)) {
					  $object = S3::getObject($awsBucket, $object['name'], JPATH_SITE . DS . 'images' . DS . 'artsexylightbox_tmp' . DS . $awsBucket . DS . $newName);
					}
				  }
			} else {
			  if ($this->isImage($object['name'])) {
				$newName = str_replace('/', '_', $object['name']);
				if (!file_exists(JPATH_SITE . DS . 'images' . DS . 'artsexylightbox_tmp' . DS . $awsBucket . DS . $newName)) {
				  $object = S3::getObject($awsBucket, $object['name'], JPATH_SITE . DS . 'images' . DS . 'artsexylightbox_tmp' . DS . $awsBucket . DS . $newName);
				}
			  }
			}
        }
        $path = 'images' . DS . 'artsexylightbox_tmp' . DS . $awsBucket;
        $directory_stream = @ opendir (JPATH_SITE . DS . $path . DS); 
        if (!$directory_stream) {
          return "Could not open a directory stream for <i>" . JPATH_SITE . DS . $path . DS . "</i>";
        }
        $filelist = array();
        while ($entry = readdir ($directory_stream)) {
          if ($entry != '.' && $entry != '..' && plgContentartsexylightbox::isImage($path . $entry)) {
            $filelist[] = $entry;
          }
        }
        $path = str_replace(DS, '/', $path);
        /** Sort images **/
        if ($sort == 'desc') {
          usort ($filelist, 'artSLFileDescSort');
        } else {
          usort ($filelist, 'artSLFileAscSort');
        }
        if ($thumbnailPreviewCount && ($thumbnailPreviewCount > count($filelist))) {
          $thumbnailPreviewCount = count($filelist);
        }
        $o = 0;
        $jsCode .= 'if (!window.sexylightboxEnabled) {SexyLightbox.initialize(' . $dFP . ');} if (!window.sexylightboxEnabled) {window.sexylightboxEnabled = true;}';
				while ((list ($key, $entry) = each ($filelist)) && ($numberOfImages > 0 ? ($imagesCount < $numberOfImages) : 1==1)) {
					if ($entry != '.' && $entry != '..' && plgContentartsexylightbox::isImage($path . $entry)) {
						$title = '';
						if (isset($descriptionArray[$entry])) {
							$title = $descriptionArray[$entry];
						}
						if ($caption) {
							$imagesCode .= "<div class='asl_image_caption'>";
						}
						if ($singleImage && $o == 0 && $showSingleImage != 'false') {
							$ind1 = strrpos($singleImage, '/');
							$sI = substr($singleImage, $ind1 + 1);
							if (isset($descriptionArray[$sI])) {
								$title = $descriptionArray[$sI];
							}
							$imagesCode .= "<a href='" . JURI::root() . "$entry";
							$imagesCode .= "' rel='sexylightbox[" . $id . "]' class='artsexylightboxpreview' ";
						} else {
							$imagesCode .= "<a href='" . JURI::root() . "$path/$entry";
							$imagesCode .= "' rel='sexylightbox[" . $id . "]' class='artsexylightboxpreview' ";
						}
						$o++;
						if ($title) {
							$imagesCode .= " title='" . $title . "'";
						}
						if ($singleImage) {
								$imagesCode .= " style='display:none;'></a>";
						} else {
							$imagesCode .= ">";
							if ($pAlt) {
								$alt = $pAlt;
							} else if ($title) {
								$alt = $title;
							} else if ($path) {
								$ind = strrpos($path, '/');
								$alt = substr($path, $ind + 1);
							} else {
								$alt = 'artsexylightbox';
							}
							$imagesCode .= "<img alt='" . $alt . "' class='artsexylightbox' ";
							if ($thumbnailPreviewCount || $singleText) {
								$imagesCode .= " style='display:none;'";
							}
							$imagesCode .= " src='"; 
							$imagePath = JPATH_SITE . DS . $path . DS . $entry;
							if ($thumbs && (isset($thumbs[$entry]) || isset($thumbs[(substr($thumbs[$entry], strrpos($thumbs[$entry], '.'))) . '.jpg']) && plgContentartsexylightbox::isFVL($entry))) {
								$imagesCode .= JURI::root() . "$thumbnailPath/$entry";
								$imagesCode .= "' ";
							} else if (@is_readable($imagePath) && ($previewWidth > 0 || $previewHeight > 0 || strstr($convertImage, 'rotate')) && isset($autoGenerateThumbs) && $autoGenerateThumbs != 'false') {
								if (!plgContentartsexylightbox::isExtensionsLoaded()) {
									plgContentartsexylightbox::loadExtensions();
								}
								if (!plgContentartsexylightbox::isExtensionsLoaded()) {
									$imagesCode .= JURI::root() . "$path/$entry' ";
								} else {
									$imagePathInfo = pathinfo($imagePath);
									$generatedThumbName = sprintf('%s_%s_%s.%s',
										md5('artsexythumb_' . $convertImage . '_' . $imagePath),
										$previewWidth,
										$previewHeight,
										$imagePathInfo['extension']);
									if (!file_exists(JPATH_SITE . DS . 'images' . DS . 'artsexylightbox_tmp')) {
										mkdir (JPATH_SITE . DS . 'images' . DS . 'artsexylightbox_tmp');
									}
									$thumbPath = JPATH_SITE . DS . 'images' . DS . 'artsexylightbox_tmp' . DS . $generatedThumbName;
									
									if (!@file_exists($thumbPath)) {
										$img = asido::image($imagePath, $thumbPath);
										if ($convertImage == 'crop' && $previewWidth && $previewHeight) {
										  if ($cropStart) {
											$cropStartArray = explode(',', $cropStart);
											Asido::crop($img, $cropStartArray[0], $cropStartArray[1], $previewWidth, $previewHeight);
										  } else {
											Asido::crop($img, 0, 0, $previewWidth, $previewHeight);
										  }
										} else if ($convertImage == 'crop_resize' && $previewWidth && $previewHeight) {
											$imgSize = getimagesize($imagePath);
											$imgWidth = $imgSize[0];
											$imgHeight = $imgSize[1];
											if ($imgWidth > $imgHeight) {
												$diff = ($imgWidth - $imgHeight)/2;
												Asido::crop($img, $diff, 0, ($imgWidth - 2*$diff), $imgHeight);
											} else if ($imgWidth < $imgHeight) {
												$diff = ($imgHeight - $imgWidth)/2;
												Asido::crop($img, 0, $diff, $imgWidth, ($imgHeight - 2*$diff));
											}
											Asido::resize($img, $previewWidth, $previewHeight, ASIDO_RESIZE_STRETCH);
										} else if (strstr($convertImage, 'rotate')) {
											$rotateAttrs = explode(':', $convertImage);
											if (!$rotateAttrs[1]) {
												$rotateAttrs[1] = 90;
											}
											Asido::Rotate($img, $rotateAttrs[1]);
										} if ($convertImage == 'grayscale') {
											Asido::GreyScale($img);
										} else {
											if (!$previewWidth) {
												Asido::height($img, $previewHeight);
											} else if (!$previewHeight) {
												Asido::width($img, $previewWidth);
											} else {
												Asido::resize($img, $previewWidth, $previewHeight, ASIDO_RESIZE_STRETCH);
											}
										}
										$img->save(ASIDO_OVERWRITE_ENABLED);
									}
									$imagesCode .= JURI::root() . "images/artsexylightbox_tmp/$generatedThumbName' ";
								}
							} else {
								$imagesCode .= JURI::root() . "$path/$entry' ";
							}
							if ($previewHeight) {
								$imagesCode .= " height='$previewHeight' ";
							}
							if ($previewWidth) {
								$imagesCode .= "width='$previewWidth' ";
							}
							$imagesCode .= "/></a>";
							if ($caption) {
								$title = str_replace('&lt;', '<', $title);
								$title = str_replace('&gt;', '>', $title);
								$imagesCode .= '<div class="asl_caption">' . html_entity_decode($title) . '</div>';
								$imagesCode .= '</div>';
							}
						}
					}
					$imagesCount++;
				}
				if (isset($popup) && $popup == 'false') {
					return $imagesCode;
				}
				$jsCode .= '})});</script>';
				$imagesCode .= '</div>';
				return $imagesCode.$jsCode;
      } else {
        echo 'Amazon Web Services: No such bucket: ' . $awsBucket;
      }
    } else if ($finalParams['fileList']) {
      $file_handle = @fopen(JPATH_SITE . DS . str_replace('/', DS, $finalParams['fileList']), "rb");
      $finalParams['fileList'] = '';
      $dFP = !empty($finalParams) ? $jsonHandler->encode($finalParams) : '';
      $jsCode .= 'if (!window.sexylightboxEnabled) {SexyLightbox.initialize(' . $dFP . ');} if (!window.sexylightboxEnabled) {window.sexylightboxEnabled = true;}';
			$descriptionArray = array();
			$filelist = array();
			if ($file_handle) {
				while (!feof($file_handle)) {
					$line_of_text = fgets($file_handle);
          if ($line_of_text != "\n" && $line_of_text != "\r" && $line_of_text != "\r\n" && $line_of_text != "") {
            $filelist[] = $line_of_text;
          }
				}
				fclose($file_handle);
			}
			
			$o = 0;
				while ((list ($key, $entry) = each ($filelist))) {
					if ($entry != '.' && $entry != '..') {
						$title = '';
						if (isset($descriptionArray[$entry])) {
							$title = $descriptionArray[$entry];
						}
						if ($caption) {
							$imagesCode .= "<div class='asl_image_caption'>";
						}
						if ($singleImage && $o == 0 && $showSingleImage != 'false') {
							$ind1 = strrpos($singleImage, '/');
							$sI = substr($singleImage, $ind1 + 1);
							if (isset($descriptionArray[$sI])) {
								$title = $descriptionArray[$sI];
							}
							$imagesCode .= "<a href='" . "$entry";
							$imagesCode .= "' rel='sexylightbox[" . $id . "]' class='artsexylightboxpreview' ";
						} else {
							$imagesCode .= "<a href='" . "$entry";
							$imagesCode .= "' rel='sexylightbox[" . $id . "]' class='artsexylightboxpreview' ";
						}
						if ( ($numberOfImages && $numberOfImages > 0) && $o >= $numberOfImages) {
							$imagesCode .= " style='display:none;' ";
						}
						$o++;
						if (isset($title)) {
							$imagesCode .= " title='" . $title . "'";
						}
						if ($singleImage) {
								$imagesCode .= " style='display:none;'></a>";
						} else {
							$imagesCode .= ">";
							if ($pAlt) {
								$alt = $pAlt;
							} else if ($title) {
								$alt = $title;
							} else if ($path) {
								$ind = strrpos($path, '/');
								$alt = substr($path, $ind + 1);
							} else {
								$alt = 'artsexylightbox';
							}
							  if (!($thumbnailPreviewCount || $singleText)) {
								$imagesCode .= "<img alt='" . $alt . "' class='artsexylightbox' ";
								$imagesCode .= " src='"; 
								$imagePath = JPATH_SITE . DS . $path . DS . $entry;
								if ($thumbs && (isset($thumbs[$entry]) || isset($thumbs[(substr($thumbs[$entry], strrpos($thumbs[$entry], '.'))) . '.jpg']) && plgContentartsexylightbox::isFVL($entry))) {
								  $imagesCode .= "$thumbnailPath/$entry";
								  $imagesCode .= "' ";
								} else if (@is_readable($imagePath) && ($previewWidth > 0 || $previewHeight > 0 || strstr($convertImage, 'rotate')) && isset($autoGenerateThumbs) && $autoGenerateThumbs != 'false') {
								  if (!plgContentartsexylightbox::isExtensionsLoaded()) {
									plgContentartsexylightbox::loadExtensions();
								  }
								  if (!plgContentartsexylightbox::isExtensionsLoaded()) {
									$imagesCode .= "$path/$entry' ";
								  } else {
									$imagePathInfo = pathinfo($imagePath);
									if ($watermark) {
									  $generatedThumbName = sprintf('%s_%s_%s.%s',
										md5('artsexythumb_' . $convertImage . '_' . $imagePath . '_' . $watermark . $watermarkPosition),
										$previewWidth,
										$previewHeight ,
										$imagePathInfo['extension']);
									} else {
									  $generatedThumbName = sprintf('%s_%s_%s.%s',
										md5('artsexythumb_' . $convertImage . '_' . $imagePath),
										$previewWidth,
										$previewHeight,
										$imagePathInfo['extension']);
									}
									if (!file_exists(JPATH_SITE . DS . 'images' . DS . 'artsexylightbox_tmp')) {
									  mkdir (JPATH_SITE . DS . 'images' . DS . 'artsexylightbox_tmp');
									}
									$thumbPath = JPATH_SITE . DS . 'images' . DS . 'artsexylightbox_tmp' . DS . $generatedThumbName;
									
									if (!@file_exists($thumbPath)) {
									  $img = asido::image($imagePath, $thumbPath);
									  if ($convertImage == 'crop' && $previewWidth && $previewHeight) {
										if ($cropStart) {
										  $cropStartArray = explode(',', $cropStart);
										  Asido::crop($img, $cropStartArray[0], $cropStartArray[1], $previewWidth, $previewHeight);
										} else {
										  Asido::crop($img, 0, 0, $previewWidth, $previewHeight);
										}
									  } else if ($convertImage == 'crop_resize' && $previewWidth && $previewHeight) {
										$imgSize = getimagesize($imagePath);
										$imgWidth = $imgSize[0];
										$imgHeight = $imgSize[1];
										if ($imgWidth > $imgHeight) {
										  $diff = ($imgWidth - $imgHeight)/2;
										  Asido::crop($img, $diff, 0, ($imgWidth - 2*$diff), $imgHeight);
										} else if ($imgWidth < $imgHeight) {
										  $diff = ($imgHeight - $imgWidth)/2;
										  Asido::crop($img, 0, $diff, $imgWidth, ($imgHeight - 2*$diff));
										}
										Asido::resize($img, $previewWidth, $previewHeight, ASIDO_RESIZE_STRETCH);
									  } else if (strstr($convertImage, 'rotate')) {
										$rotateAttrs = explode(':', $convertImage);
										if (!$rotateAttrs[1]) {
										  $rotateAttrs[1] = 90;
										}
										Asido::Rotate($img, $rotateAttrs[1]);
									  } if ($convertImage == 'grayscale') {
											Asido::GreyScale($img);
										} else {
										if (!$previewWidth) {
										  Asido::height($img, $previewHeight);
										} else if (!$previewHeight) {
										  Asido::width($img, $previewWidth);
										} else {
										  Asido::resize($img, $previewWidth, $previewHeight, ASIDO_RESIZE_STRETCH);
										}
									  }
									  if ($watermark) {
										switch($watermarkPosition) {
										  case 'topleft':
										  Asido::watermark($img, JPATH_SITE . DS . str_replace('/', DS, $watermark), ASIDO_WATERMARK_TOP_LEFT);
										  break;
										  
										  case 'topright':
										  Asido::watermark($img, JPATH_SITE . DS . str_replace('/', DS, $watermark), ASIDO_WATERMARK_TOP_RIGHT);
										  break;
										  
										  case 'bottomleft':
										  Asido::watermark($img, JPATH_SITE . DS . str_replace('/', DS, $watermark), ASIDO_WATERMARK_BOTTOM_LEFT);
										  break;
										  
										  case 'bottomright':
										  Asido::watermark($img, JPATH_SITE . DS . str_replace('/', DS, $watermark), ASIDO_WATERMARK_BOTTOM_RIGHT);
										  break;
										  
										  default:
										  Asido::watermark($img, JPATH_SITE . DS . str_replace('/', DS, $watermark), ASIDO_WATERMARK_TOP_LEFT);
										  break;
										}
									  }
									  $img->save(ASIDO_OVERWRITE_ENABLED);
									}
									$imagesCode .= "images/artsexylightbox_tmp/$generatedThumbName' ";
								  }
								} else {
								  $imagesCode .= "$entry' ";
								}
								if ($previewHeight) {
								  $imagesCode .= " height='$previewHeight' ";
								}
								if ($previewWidth) {
								  $imagesCode .= "width='$previewWidth' ";
								}
								$imagesCode .= "/>";
							  }
							$imagesCode .= "</a>";
							if ($caption) {
								$title = str_replace('&lt;', '<', $title);
								$title = str_replace('&gt;', '>', $title);
								$imagesCode .= '<div class="asl_caption">' . html_entity_decode($title) . '</div>';
								$imagesCode .= '</div>';
							}
						}
					}
					$imagesCount++;
				}
				if (isset($popup) && $popup == 'false') {
					return $imagesCode;
				}
				$jsCode .= '})});</script>';
				$imagesCode .= '</div>';
				return $imagesCode.$jsCode;
    } else if ($remoteDirListing) {
			$jsCode .= 'if (!window.sexylightboxEnabled) {SexyLightbox.initialize(' . $dFP . ');} if (!window.sexylightboxEnabled) {window.sexylightboxEnabled = true;}';
			ini_set('allow_url_fopen', 1);
			$file_handle = @fopen($remoteDirListing, "rb");
			if ($file_handle) {
				while (!feof($file_handle) ) {
					$line_of_text = fgets($file_handle);
				}
				fclose($file_handle);
			}
			$filelist = explode('<br />', $line_of_text);
			
			if ($singleImage) {
				$imagesCode .= "<a onclick='asljQuery(\"a[rel^=sexylightboxgallery]\").eq(0).click();return false;'><img alt='$pAlt' class='artsexylightbox_singleimage' src='" . JURI::root() . $singleImage . "'";
				if ($previewHeight) {
					$imagesCode .= " height='$previewHeight'";
				}
				if ($previewWidth) {
					$imagesCode .= " width='$previewWidth' ";
				}
				$imagesCode .= " /></a>";
			}
      if ($carousel) {
        $imagesCode .= '<div class="asl_prev" id="aslcarouselprev_' . $id . '"></div><div class="asl_carousel" id="aslcarousel_' . $id . '"><ul>';
        while (list ($key, $entry) = each ($filelist)) {
          if ($entry != '.' && $entry != '..' && $this->isImage($path . $entry)) {
            $imagesCode .= "<li><a href='$entry' rel='sexylightboxgallery[" . $id . "]' class='artsexylightboxpreview'>";
            if ($singleImage) {
              $imagesCode .= "</a></li>";
            } else {
              $imagesCode .= "<img alt='$pAlt' class='artsexylightbox' src='$entry' ";
              if ($previewHeight) {
                  $imagesCode .= " height='$previewHeight' ";
              }
              if ($previewWidth) {
                  $imagesCode .= "width='$previewWidth' ";
              }
              $imagesCode .= "/></a></li>";
            }
          }
        }
        $imagesCode .= '</ul></div><div id="aslcarouselnext_' . $id . '" class="asl_next"></div><div style="clear:both"></div>';
		$jsCode .= 'asljQuery(function() {asljQuery("#aslcarousel_' . $id . '").jCarouselLite({vertical: ' . $vertical . ', circular: false, visible: ' . $carousel_visible . ', scroll: ' . $carousel_scroll . ', btnNext: "#aslcarouselnext_' . $id . '", btnPrev: "#aslcarouselprev_' . $id . '"});});';
      } else {
        while (list ($key, $entry) = each ($filelist)) {
          if ($entry != '.' && $entry != '..' && $this->isImage($path . $entry)) {
            $imagesCode .= "<a href='$entry' rel='sexylightboxgallery[" . $id . "]' class='artsexylightboxpreview'>";
            if ($singleImage) {
              $imagesCode .= "</a>";
            } else {
              $imagesCode .= "<img alt='$pAlt' class='artsexylightbox' src='$entry' ";
              if ($previewHeight) {
                  $imagesCode .= " height='$previewHeight' ";
              }
              if ($previewWidth) {
                  $imagesCode .= "width='$previewWidth' ";
              }
              $imagesCode .= "/></a>";
            }
          }
        }
      }
			$jsCode .= '})});</script>';
			$imagesCode .= '</div>';
			return $imagesCode.$jsCode;
		
		/** Read images from local folder **/
		} else if ($article) {
      $db	=& JFactory::getDBO();
			$query = 'SELECT * FROM #__content WHERE id = ' . intval($article);
      $db->setQuery($query);
			$row = $db->loadObject();
      $jsCode .= 'if (!window.sexylightboxEnabled) {SexyLightbox.initialize(' . $dFP . ');} if (!window.sexylightboxEnabled) {window.sexylightboxEnabled = true;}';
      if ($autoLoad) {
				$jsCode .= 'SexyLightbox.hook("a.artsexylightboxpreview");';
			}
      if ($autoClose) {
        $jsCode .= 'window.setTimeout(function(){SexyLightbox.close();}, ' . $autoClose . ')';
      }
      
      $id = intval($article);
			$url = "#TB_inline?inlineId=$id";
			if (isset($height) && !empty($height)) $url .= "&height=$height";
			if (isset($width) && !empty($width)) $url .= "&width=$width";
			if (isset($modal) && !empty($modal)) $url .= "&modal=$modal";
			if ($singleImage) {
				$text = "<img alt='$pAlt' class='artsexylightbox_singleimage' src='". $singleImage ."'";
				if ($previewHeight) {
					$text .= " height='$previewHeight' ";
				}
				if ($previewWidth) {
					$text .= "width='$previewWidth' ";
				}
				$text .= "/>";
			} else if ($singleText) {
				$text = $singleText;
			}
      $imagesCode .= "<span id='$id'><span class='article_content'>" . $row->introtext . "</span></span><a class='artsexylightboxpreview' href='$url' rel='sexylightbox' title='$title'>$text</a>";
      $jsCode .= 'document.getElementById("' . $id . '").style.display = "none";';
			$jsCode .= '})});</script>';
      $imagesCode .= '</div>';
      return $imagesCode.$jsCode;
    } else if ($link) {
			$jsCode .= 'if (!window.sexylightboxEnabled) {SexyLightbox.initialize(' . $dFP . ');} if (!window.sexylightboxEnabled) {window.sexylightboxEnabled = true;}';
			if ($autoLoad) {
				$jsCode .= 'SexyLightbox.hook("a.artsexylightboxpreview");';
			}
			  if ($autoClose) {
				$jsCode .= 'window.setTimeout(function(){SexyLightbox.close();}, ' . $autoClose . ')';
			  }
      
			$url = $link . "?TB_iframe=true";
			if (isset($height) && !empty($height)) $url .= "&height=$height";
			if (isset($width) && !empty($width)) $url .= "&width=$width";
			if (isset($modal) && !empty($modal)) $url .= "&modal=$modal";
			if ($singleImage) {
				$text = "<img alt='$pAlt' class='artsexylightbox_singleimage' src='". $singleImage ."'";
				if ($previewHeight) {
					$text .= " height='$previewHeight' ";
				}
				if ($previewWidth) {
					$text .= "width='$previewWidth' ";
				}
				$text .= "/>";
			} else if ($singleText) {
				$text = $singleText;
			}
		  if (!$title) {
			$title = '';
		  }
      $imagesCode .= '<a class="artsexylightboxpreview" href="' . $url . '" rel="sexylightbox" title="' . $title .'">' . $text . '</a>';
			$jsCode .= '})});</script>';
      $imagesCode .= '</div>';
      return $imagesCode.$jsCode;
    } if ($image) {
      $jsCode .= 'if (!window.sexylightboxEnabled) {SexyLightbox.initialize(' . $dFP . ');} if (!window.sexylightboxEnabled) {window.sexylightboxEnabled = true;}';
      if ($autoLoad) {
				$jsCode .= 'SexyLightbox.hook("a.artsexylightboxpreview");';
			}
      if ($autoClose) {
        $jsCode .= 'window.setTimeout(function(){SexyLightbox.close();}, ' . $autoClose . ')';
      }
      $imagesCode .= "<a class='artsexylightboxpreview' rel='sexylightbox' href='" . JURI::root() . $image;
      if ($width & $height) {
        $imagesCode .= "?width=$width&height=$height";
      }
      $imagesCode .= "'";
      if ($title) {
        $imagesCode .= " title='" . $title . "'";
      } else {
        $imagesCode .= " title=' '";
      }
      if ($hideContent) {
        $imagesCode .= " style='display:none'";
      }
      if ($singleText) {
        $imagesCode .= ">" . $singleText . "</a>";
      } else {
        $imagesCode .= ">";
        if ($thumbImage) {
          $imagesCode .= "<img alt='$pAlt' class='artsexylightbox_singleimage' src='" . JURI::root() . $thumbImage . "'";
        } else {
          $imagesCode .= "<img alt='$pAlt' class='artsexylightbox_singleimage' src='" . JURI::root() . $image . "'";
          if ($previewHeight) {
            $imagesCode .= " height='$previewHeight'";
          }
          if ($previewWidth) {
            $imagesCode .= " width='$previewWidth' ";
          }
        }
        $imagesCode .= " /></a>";
        }
      $jsCode .= '})});</script>';
      $imagesCode .= '</div>';
      return $imagesCode.$jsCode;
    } else if ($youtube) {
      $jsCode .= 'if (!window.sexylightboxEnabled) {SexyLightbox.initialize(' . $dFP . ');} if (!window.sexylightboxEnabled) {window.sexylightboxEnabled = true;}';
      if (!$height) {
        $height = 600;
      }
      if (!$width) {
        $width = 800;
      }
      $imagesCode .= "<a href='http://www.youtube.com/embed/" . $youtube . "?&TB_iframe=true&amp;height=" . $height . "&amp;width=" . $width . "' rel='sexylightbox'";
      if ($title) {
        $imagesCode .= " title='" . $title . "'";
      } else {
        $imagesCode .= " title=' '";
      }
      if ($singleText) {
        $imagesCode .= ">" . $singleText . "</a>";
      } else {
        $imagesCode .= ">";
        if ($thumbImage) {
          $imagesCode .= "<img alt='$pAlt' class='artsexylightbox_singleimage' src='" . JURI::root() . $thumbImage . "'";
          if ($previewHeight) {
            $imagesCode .= " height='$previewHeight'";
          }
          if ($previewWidth) {
            $imagesCode .= " width='$previewWidth' ";
          }
        } else {
          $imagesCode .= "<img alt='$pAlt' class='artsexylightbox_singleimage' src='http://img.youtube.com/vi/" . $youtube . "/default.jpg' ";
          if ($previewHeight) {
            $imagesCode .= " height='$previewHeight'";
          }
          if ($previewWidth) {
            $imagesCode .= " width='$previewWidth' ";
          }
        }
        $imagesCode .= " /></a>";
        }
      $jsCode .= '})});</script>';
      $imagesCode .= '</div>';
      return $imagesCode.$jsCode;
    } else if ($module) {
      $document	= &JFactory::getDocument();
      $renderer	= $document->loadRenderer('module');

      $params		= array();
      $contents = '';
      foreach (JModuleHelper::getModules($module) as $mod)  {
        $contents .= $renderer->render($mod, $params);
      }
      $id = uniqid('artinline_', false);
		$url = "#TB_inline?inlineId=$id";
		if (isset($height) && !empty($height)) $url .= "&height=$height";
		if (isset($width) && !empty($width)) $url .= "&width=$width";
		if (isset($modal) && !empty($modal)) $url .= "&modal=$modal";
		if ($singleImage) {
			$text = "<img alt='$pAlt' class='artsexylightbox_singleimage' src='". $singleImage ."'";
			if ($previewHeight) {
				$text .= " height='$previewHeight' ";
			}
			if ($previewWidth) {
				$text .= "width='$previewWidth' ";
			}
			$text .= "/>";
		} else if ($singleText) {
			$text = $singleText;
		}
		$inlineCode = "<span id='$id' style='display:none;'>$contents</span><a href='$url' rel='sexylightbox' title='$title'>$text</a>";
		$jsCode .= 'SexyLightbox.initialize(' . $dFP . ');';
		$jsCode .= 'if (!window.sexylightboxEnabled) {window.sexylightboxEnabled = true;}';
		$jsCode .= '})});</script>';
		return $jsCode.$inlineCode;
    } else if ($path) {
			if ($thumbnailPath) {
				$thumbs = array();
				$thumb_directory_stream = @ opendir (JPATH_SITE.DS . $thumbnailPath . DS);
				if ($thumb_directory_stream) {
					while ($entry = readdir ($thumb_directory_stream)) {
						if ($entry != '.' && $entry != '..' && plgContentartsexylightbox::isImage($thumbnailPath . $entry)) {
							$thumbs[$entry] = $entry;
						}
					}
				}
			}
			/** Read image descriptions **/
			if ($lang2 && file_exists(JPATH_SITE . DS . $path . DS . 'artsexylightbox_' . $lang2 . '.txt')) {
				$file_handle = @fopen(JPATH_SITE . DS . $path . DS . 'artsexylightbox_' . $lang2 . '.txt', "rb");
			} else if (JRequest::getString('lang') && file_exists(JPATH_SITE . DS . $path . DS . 'artsexylightbox_' . JRequest::getString('lang') . '.txt')) {
				$file_handle = @fopen(JPATH_SITE . DS . $path . DS . 'artsexylightbox_' . JRequest::getString('lang') . '.txt', "rb");
			} else if ($this && $this->language && file_exists(JPATH_SITE . DS . $path . DS . 'artsexylightbox_' . $this->language . '.txt')) {
				$file_handle = @fopen(JPATH_SITE . DS . $path . DS . 'artsexylightbox_' . $this->language . '.txt', "rb");
			} else {
				$file_handle = @fopen(JPATH_SITE . DS . $path . DS . 'artsexylightbox.txt', 'rb');
			}
			$descriptionArray = array();
			if ($file_handle) {
				while (!feof($file_handle) ) {
					$line_of_text = fgets($file_handle);
					$parts = explode('=', htmlspecialchars($line_of_text, ENT_QUOTES));
					$str = '';
					$partsNumber = count($parts);
					for ($i = 1; $i < $partsNumber; $i++) {
						$str .= $parts[$i];
						if ($i != $partsNumber - 1) {
							$str .= '=';
						}
					}
					$str = str_replace('"', "'", $str);
					$descriptionArray[$parts[0]] = html_entity_decode($str);

				}
				fclose($file_handle);
			}
			
			$jsCode .= 'if (!window.sexylightboxEnabled) {SexyLightbox.initialize(' . $dFP . ');} if (!window.sexylightboxEnabled) {window.sexylightboxEnabled = true;}';
			$directory_stream = @ opendir (JPATH_SITE.DS . $path . DS); 
			if (!$directory_stream) {
				return "Could not open a directory stream for <i>" . JPATH_SITE . DS . $path . DS . "</i>";
			}
			$filelist = array();
			while ($entry = readdir ($directory_stream)) {
				if ($entry != '.' && $entry != '..' && plgContentartsexylightbox::isImage($path . $entry)) {
          if ($sortBy == 'date') {
            if (!$pattern) {
              $filelist[filemtime(JPATH_SITE . DS . str_replace('/', DS, $path) . DS . $entry)] = $entry;
            } else if ($pattern && preg_match("/" . $pattern . "/i", $entry)) {
              $filelist[filemtime(JPATH_SITE . DS . str_replace('/', DS, $path) . DS . $entry)] = $entry;
            }
          } else {
            if (!$pattern) {
              $filelist[] = $entry;
            } else if ($pattern && preg_match("/" . $pattern . "/i", $entry)) {
              $filelist[] = $entry;
            }
          }
				}
			}
			/** Sort images **/
      if ($sortBy == 'date') {
        ksort($filelist);
        if ($sort == 'desc') {
          $filelist = array_reverse($filelist);
        }
      } else if ($sort == 'desc' && $sortBy == 'title') {
				usort ($filelist, 'artSLFileDescSort');
			} else if ($sort == 'asc' && $sortBy == 'title') {
				usort ($filelist, 'artSLFileAscSort');
			} else if ($sort == 'random') {
          shuffle($filelist);
      }
			if ($thumbnailPreviewCount && ($thumbnailPreviewCount > count($filelist))) {
				$thumbnailPreviewCount = count($filelist);
			}
			if ($singleImage) {
				if ($caption) {
					$imagesCode .= "<div class='asl_image_caption'>";
				}
				if ($singleImage != 'random') {
					$imagesCode .= "<a onclick='asljQuery(\"a[rel*=" . $id . "]\").eq(0).click();return false;'><img alt='$pAlt' class='artsexylightbox_singleimage' src='" . JURI::root() . $singleImage . "'";
					if ($previewHeight) {
						$imagesCode .= " height='$previewHeight'";
					}
					if ($previewWidth) {
						$imagesCode .= " width='$previewWidth' ";
					}
					$imagesCode .= " /></a>";
				} else {
					mt_srand((double)microtime()*1000000);
					$rand = mt_rand(0, count($filelist) - 1);
					$imagesCode .= "<a onclick='asljQuery(\"a[rel*=" . $id . "]\").eq(0).click();return false;'><img alt='$pAlt' class='artsexylightbox_singleimage' src='" . JURI::root() . $path. '/' . $filelist[$rand] . "'";
					if ($previewHeight) {
						$imagesCode .= " height='$previewHeight'";
					}
					if ($previewWidth) {
						$imagesCode .= " width='$previewWidth' ";
					}
					$imagesCode .= " /></a>";
				}
				if ($caption) {
					$title = str_replace('&lt;', '<', $title);
					$title = str_replace('&gt;', '>', $title);
          $ind1 = strrpos($singleImage, '/');
          $sI = substr($singleImage, $ind1 + 1);
          if (isset($descriptionArray[$sI])) {
            $title = $descriptionArray[$sI];
          }
					$imagesCode .= '<div class="asl_caption">' . html_entity_decode($title) . '</div>';
					$imagesCode .= '</div>';
				}
			} else if ($singleText) {
				$imagesCode .= "<a onclick='asljQuery(\"a[rel*=" . $id . "]\").eq(0).click();return false;'><span class='artsexylightbox_singletext'>" . $singleText . "</span></a>";
			} else if ($thumbnailPreviewCount && $thumbnailPreviewCount > 0) {
				$thumbsCount = 0;
				while ((list ($key, $entry) = each ($filelist)) && ($thumbsCount < $thumbnailPreviewCount)) {
					if ($entry != '.' && $entry != '..' && plgContentartsexylightbox::isImage($path . $entry)) {
						$imagePath = JPATH_SITE . DS . $path . DS . $entry;
						if ($thumbs && isset($thumbs[$entry])) {
							$imagesCode .= "<a onclick='asljQuery(\"a[rel*=" . $id . "]\").eq(0).click();return false;'><img alt='$pAlt' class='artsexylightbox_singleimage' src='" . JURI::root() . $thumbnailPath . '/' . $entry . "'";
						} else {
							$imagesCode .= "<a onclick='asljQuery(\"a[rel*=" . $id . "]\").eq(0).click();return false;'><img alt='$pAlt' class='artsexylightbox_singleimage' src='" . JURI::root() . $path . '/' . $entry . "'";
						}
						if ($previewHeight) {
							$imagesCode .= " height='$previewHeight'";
						}
						if ($previewWidth) {
							$imagesCode .= " width='$previewWidth' ";
						}
						$imagesCode .= " /></a>";
						$thumbsCount++;
					}
				}
			}
			reset ($filelist);
			$imagesCount = 0; 
			if ($singleImage) {
				$nfilelist = array();
				if ($showSingleImage != 'false') {
					if ($singleImage != 'random') {
						$nfilelist[] = $singleImage;
					} else {
						$nfilelist[] = $path. '/' .$filelist[$rand];
					}
				}
				while ((list ($key, $entry) = each ($filelist))) {
					if (($path . '/' . $entry) != $singleImage) {
						$nfilelist[] = $entry;
					}
				}
				reset ($nfilelist);
				$filelist = $nfilelist;
			}
			/** Show images as carousel **/
		  if ($preload) {
			$imagesCode .= '<div style="display:none">';
			while ((list ($key, $entry) = each ($filelist)) && ($numberOfImages > 0 ? ($imagesCount < $numberOfImages) : 1==1)) {
						if ($entry != '.' && $entry != '..' && plgContentartsexylightbox::isImage($path . $entry)) {
				$imagesCode .= '<img src="' . JURI::root() . "$path/$entry" . '" alt="image"/>';
			  }
			}
			$imagesCode .= '</div>';
			reset ($filelist);
		  }
			if ($carousel && $path) {
				$imagesCode .= '<div class="asl_prev" id="aslcarouselprev_' . $id . '"></div><div class="asl_carousel" id="aslcarousel_' . $id . '"><ul>';
				while ((list ($key, $entry) = each ($filelist)) && ($numberOfImages > 0 ? ($imagesCount < $numberOfImages) : 1==1)) {
					if ($entry != '.' && $entry != '..' && plgContentartsexylightbox::isImage($path . $entry)) {
						$title = '';
						if (isset($descriptionArray[$entry])) {
							$title = $descriptionArray[$entry];
						}
						$imagesCode .= "<li><a href='" . JURI::root() . "$path/$entry";
						$imagesCode .= "' rel='sexylightbox[" . $id . "]' class='artsexylightboxpreview' ";
						if ($title) {
							$imagesCode .= " title='" . $title . "'";
						}
						if ($singleImage) {
							$imagesCode .= " style='display:none;'></a>";
						} else {
							$imagesCode .= ">";
							if ($pAlt) {
								$alt = $pAlt;
							} else if ($title) {
								$alt = $title;
							} else if ($path) {
								$ind = strrpos($path, '/');
								$alt = substr($path, $ind);
							} else {
								$alt = 'artsexylightbox';
							}
							$imagesCode .= "<img alt='" . $alt . "' class='artsexylightbox' ";
							if ($thumbnailPreviewCount || $singleText) {
								$imagesCode .= " style='display:none;'";
							}
							$imagesCode .= " src='"; 
							$imagePath = JPATH_SITE . DS . $path . DS . $entry;
							if ($thumbs && isset($thumbs[$entry])) {
								$imagesCode .= JURI::root() . "$thumbnailPath/$entry' ";
							} else if (@is_readable($imagePath) && ($previewWidth > 0 || $previewHeight > 0 || strstr($convertImage, 'rotate')) && isset($autoGenerateThumbs) && $autoGenerateThumbs != 'false') {
								if (!plgContentartsexylightbox::isExtensionsLoaded()) {
									plgContentartsexylightbox::loadExtensions();
								}
								if (!plgContentartsexylightbox::isExtensionsLoaded()) {
									$imagesCode .= JURI::root() . "$path/$entry' ";
								} else {
									$imagePathInfo = pathinfo($imagePath);
									$generatedThumbName = sprintf('%s_%s_%s.%s',
										md5('artsexythumb_' . $convertImage . '_' . $imagePath),
										$previewWidth,
										$previewHeight,
										$imagePathInfo['extension']);
									if (!file_exists(JPATH_SITE . DS . 'images' . DS . 'artsexylightbox_tmp')) {
										mkdir (JPATH_SITE . DS . 'images' . DS . 'artsexylightbox_tmp');
									}
									$thumbPath = JPATH_SITE . DS . 'images' . DS . 'artsexylightbox_tmp' . DS . $generatedThumbName;
									
									if (!@file_exists($thumbPath)) {
										$img = asido::image($imagePath, $thumbPath);
										if ($convertImage == 'crop' && $previewWidth && $previewHeight) {
											if ($cropStart) {
												$cropStartArray = explode(',', $cropStart);
												Asido::crop($img, $cropStartArray[0], $cropStartArray[1], $previewWidth, $previewHeight);
											  } else {
												Asido::crop($img, 0, 0, $previewWidth, $previewHeight);
											  }
											} else if ($convertImage == 'crop_resize' && $previewWidth && $previewHeight) {
											$imgSize = getimagesize($imagePath);
											$imgWidth = $imgSize[0];
											$imgHeight = $imgSize[1];
											if ($imgWidth > $imgHeight) {
												$diff = ($imgWidth - $imgHeight)/2;
												Asido::crop($img, $diff, 0, ($imgWidth - 2*$diff), $imgHeight);
											} else if ($imgWidth < $imgHeight) {
												$diff = ($imgHeight - $imgWidth)/2;
												Asido::crop($img, 0, $diff, $imgWidth, ($imgHeight - 2*$diff));
											}
											Asido::resize($img, $previewWidth, $previewHeight, ASIDO_RESIZE_STRETCH);
										} else if (strstr($convertImage, 'rotate')) {
											$rotateAttrs = explode(':', $convertImage);
											if (!$rotateAttrs[1]) {
												$rotateAttrs[1] = 90;
											}
											Asido::Rotate($img, $rotateAttrs[1]);
										} if ($convertImage == 'grayscale') {
											Asido::GreyScale($img);
										} else {
											if (!$previewWidth) {
												Asido::height($img, $previewHeight);
											} else if (!$previewHeight) {
												Asido::width($img, $previewWidth);
											} else {
												Asido::resize($img, $previewWidth, $previewHeight, ASIDO_RESIZE_STRETCH);
											}
										}
										$img->save(ASIDO_OVERWRITE_ENABLED);
									}
									$imagesCode .= JURI::root() . "images/artsexylightbox_tmp/$generatedThumbName' ";
								}
							} else {
								$imagesCode .= JURI::root() . "$path/$entry' ";
							}
							if ($previewHeight) {
								$imagesCode .= " height='$previewHeight' ";
							}
							if ($previewWidth) {
								$imagesCode .= "width='$previewWidth' ";
							}
							$imagesCode .= "/></a></li>";
						}
					}
					$imagesCount++;
				}
				$imagesCode .= '</ul></div><div id="aslcarouselnext_' . $id . '" class="asl_next"></div><div style="clear:both"></div>';
				$jsCode .= '})});asljQuery(function() {asljQuery("#aslcarousel_' . $id . '").jCarouselLite({vertical: ' . $vertical . ', circular: false ';
        if ($auto) {
          $jsCode .= ',auto:' . $auto . ',speed:' . $speed . '';
        }
        $jsCode .= ',visible: ' . $carousel_visible . ', scroll: ' . $carousel_scroll . ', btnNext: "#aslcarouselnext_' . $id . '", btnPrev: "#aslcarouselprev_' . $id . '"});});</script>';
				$imagesCode .= '</div>';
				if (isset($popup) && $popup == 'false') {
					$jsCode = '<script type="text/javascript" charset="utf-8">asljQuery(function() {asljQuery("#aslcarousel_' . $id . '").jCarouselLite({vertical: ' . $vertical . ', circular: false, visible: ' . $carousel_visible . ', scroll: ' . $carousel_scroll . ', btnNext: "#aslcarouselnext_' . $id . '", btnPrev: "#aslcarouselprev_' . $id . '"});});</script>';
					return $imagesCode.$jsCode;
				}
				return $imagesCode.$jsCode;
			} else if ($cloudCarousel) {
				/** Show gallery from local folder **/
				$o = 0;
				while ((list ($key, $entry) = each ($filelist)) && ($numberOfImages > 0 ? ($imagesCount < $numberOfImages) : 1==1)) {
					if ($entry != '.' && $entry != '..' && plgContentartsexylightbox::isImage($path . $entry)) {
						$title = '';
						if (isset($descriptionArray[$entry])) {
							$title = $descriptionArray[$entry];
						}
						if ($caption) {
							$imagesCode .= "<div class='asl_image_caption'>";
						}
						if ($singleImage && $o == 0 && $showSingleImage != 'false') {
							$ind1 = strrpos($singleImage, '/');
							$sI = substr($singleImage, $ind1 + 1);
							if (isset($descriptionArray[$sI])) {
								$title = $descriptionArray[$sI];
							}
							$imagesCode .= "<a href='" . JURI::root() . "$entry";
							$imagesCode .= "' rel='sexylightbox[" . $id . "]' class='artsexylightboxpreview' ";
						} else {
							$imagesCode .= "<a href='" . JURI::root() . "$path/$entry";
							$imagesCode .= "' rel='sexylightbox[" . $id . "]' class='artsexylightboxpreview' ";
						}
						$o++;
						if ($title) {
							$imagesCode .= " title='" . $title . "'";
						}
						if ($singleImage) {
								$imagesCode .= " style='display:none;'></a>";
						} else {
							$imagesCode .= ">";
							if ($pAlt) {
								$alt = $pAlt;
							} else if ($title) {
								$alt = $title;
							} else if ($path) {
								$ind = strrpos($path, '/');
								$alt = substr($path, $ind + 1);
							} else {
								$alt = 'artsexylightbox';
							}
							$imagesCode .= "<img alt='" . $alt . "' class='artsexylightbox cloudcarousel' ";
							if ($title) {
								$imagesCode .= " title='" . $title . "'";
							}
							if ($thumbnailPreviewCount || $singleText) {
								$imagesCode .= " style='display:none;'";
							}
							$imagesCode .= " src='"; 
							$imagePath = JPATH_SITE . DS . $path . DS . $entry;
							if ($thumbs && isset($thumbs[$entry])) {
								$imagesCode .= JURI::root() . "$thumbnailPath/$entry";
								$imagesCode .= "' ";
							} else if (@is_readable($imagePath) && ($previewWidth > 0 || $previewHeight > 0 || strstr($convertImage, 'rotate')) && isset($autoGenerateThumbs) && $autoGenerateThumbs != 'false') {
								if (!plgContentartsexylightbox::isExtensionsLoaded()) {
									plgContentartsexylightbox::loadExtensions();
								}
								if (!plgContentartsexylightbox::isExtensionsLoaded()) {
									$imagesCode .= JURI::root() . "$path/$entry' ";
								} else {
									$imagePathInfo = pathinfo($imagePath);
									$generatedThumbName = sprintf('%s_%s_%s.%s',
										md5('artsexythumb_' . $convertImage . '_' . $imagePath),
										$previewWidth,
										$previewHeight,
										$imagePathInfo['extension']);
									if (!file_exists(JPATH_SITE . DS . 'images' . DS . 'artsexylightbox_tmp')) {
										mkdir (JPATH_SITE . DS . 'images' . DS . 'artsexylightbox_tmp');
									}
									$thumbPath = JPATH_SITE . DS . 'images' . DS . 'artsexylightbox_tmp' . DS . $generatedThumbName;
									
									if (!@file_exists($thumbPath)) {
										$img = asido::image($imagePath, $thumbPath);
										if ($convertImage == 'crop' && $previewWidth && $previewHeight) {
										if ($cropStart) {
											$cropStartArray = explode(',', $cropStart);
											Asido::crop($img, $cropStartArray[0], $cropStartArray[1], $previewWidth, $previewHeight);
										  } else {
											Asido::crop($img, 0, 0, $previewWidth, $previewHeight);
										  }
										} else if ($convertImage == 'crop_resize' && $previewWidth && $previewHeight) {
											$imgSize = getimagesize($imagePath);
											$imgWidth = $imgSize[0];
											$imgHeight = $imgSize[1];
											if ($imgWidth > $imgHeight) {
												$diff = ($imgWidth - $imgHeight)/2;
												Asido::crop($img, $diff, 0, ($imgWidth - 2*$diff), $imgHeight);
											} else if ($imgWidth < $imgHeight) {
												$diff = ($imgHeight - $imgWidth)/2;
												Asido::crop($img, 0, $diff, $imgWidth, ($imgHeight - 2*$diff));
											}
											Asido::resize($img, $previewWidth, $previewHeight, ASIDO_RESIZE_STRETCH);
										} else if (strstr($convertImage, 'rotate')) {
											$rotateAttrs = explode(':', $convertImage);
											if (!$rotateAttrs[1]) {
												$rotateAttrs[1] = 90;
											}
											Asido::Rotate($img, $rotateAttrs[1]);
										} if ($convertImage == 'grayscale') {
											Asido::GreyScale($img);
										} else {
											if (!$previewWidth) {
												Asido::height($img, $previewHeight);
											} else if (!$previewHeight) {
												Asido::width($img, $previewWidth);
											} else {
												Asido::resize($img, $previewWidth, $previewHeight, ASIDO_RESIZE_STRETCH);
											}
										}
										$img->save(ASIDO_OVERWRITE_ENABLED);
									}
									$imagesCode .= JURI::root() . "images/artsexylightbox_tmp/$generatedThumbName' ";
								}
							} else {
								$imagesCode .= JURI::root() . "$path/$entry' ";
							}
							if ($previewHeight) {
								$imagesCode .= " height='$previewHeight' ";
							}
							if ($previewWidth) {
								$imagesCode .= "width='$previewWidth' ";
							}
							$imagesCode .= "/></a>";
							if ($caption) {
								$title = str_replace('&lt;', '<', $title);
								$title = str_replace('&gt;', '>', $title);
								$imagesCode .= '<div class="asl_caption">' . html_entity_decode($title) . '</div>';
								$imagesCode .= '</div>';
							}
						}
					}
					$imagesCount++;
				}
				if (isset($popup) && $popup == 'false') {
					return $imagesCode;
				}
				$jsCode .= 'asljQuery(".artsexylightbox_container").CloudCarousel(' . $dFP . ');})});</script>';
				$imagesCode .= '<div class="cloud_carousel_title"></div>';
				if (!$hideButtons) {
					$imagesCode .= '<div id="carousel_left' . $id . '" class="carouselLeft"></div>';
					$imagesCode .= '<div id="carousel_right' . $id . '" class="carouselRight"></div>';
				}
				$imagesCode .= '</div>';
				return $imagesCode.$jsCode;
			} else if ($autoCarousel) {
				$o = 0;
				$imagesCode .= '<ul class="rui-tabs-carousel" id="carousel-tabs' . $id . '"><ul>';
				while ((list ($key, $entry) = each ($filelist)) && ($numberOfImages > 0 ? ($imagesCount < $numberOfImages) : 1==1)) {
					if ($entry != '.' && $entry != '..' && plgContentartsexylightbox::isImage($path . $entry)) {
						$title = '';
						if (isset($descriptionArray[$entry])) {
							$title = $descriptionArray[$entry];
						}
						if ($singleImage && $o == 0 && $showSingleImage != 'false') {
							$ind1 = strrpos($singleImage, '/');
							$sI = substr($singleImage, $ind1 + 1);
							if (isset($descriptionArray[$sI])) {
								$title = $descriptionArray[$sI];
							}
							$imagesCode .= "<li><a href='" . JURI::root() . "$entry";
							$imagesCode .= "' rel='sexylightbox[" . $id . "]' class='artsexylightboxpreview' ";
						} else {
							$imagesCode .= "<li><a href='" . JURI::root() . "$path/$entry";
							$imagesCode .= "' rel='sexylightbox[" . $id . "]' class='artsexylightboxpreview' ";
						}
						$o++;
						if ($title) {
							$imagesCode .= " title='" . $title . "'";
						}
						if ($singleImage) {
								$imagesCode .= " style='display:none;'></a>";
						} else {
							$imagesCode .= ">";
							if ($pAlt) {
								$alt = $pAlt;
							} else if ($title) {
								$alt = $title;
							} else if ($path) {
								$ind = strrpos($path, '/');
								$alt = substr($path, $ind + 1);
							} else {
								$alt = 'artsexylightbox';
							}
							$imagesCode .= "<img alt='" . $alt . "' class='artsexylightbox cloudcarousel' ";
							if ($title) {
								$imagesCode .= " title='" . $title . "'";
							}
							if ($thumbnailPreviewCount || $singleText) {
								$imagesCode .= " style='display:none;'";
							}
							$imagesCode .= " src='"; 
							$imagePath = JPATH_SITE . DS . $path . DS . $entry;
							if ($thumbs && isset($thumbs[$entry])) {
								$imagesCode .= JURI::root() . "$thumbnailPath/$entry";
								$imagesCode .= "' ";
							} else if (@is_readable($imagePath) && ($previewWidth > 0 || $previewHeight > 0 || strstr($convertImage, 'rotate')) && isset($autoGenerateThumbs) && $autoGenerateThumbs != 'false') {
								if (!plgContentartsexylightbox::isExtensionsLoaded()) {
									plgContentartsexylightbox::loadExtensions();
								}
								if (!plgContentartsexylightbox::isExtensionsLoaded()) {
									$imagesCode .= JURI::root() . "$path/$entry' ";
								} else {
									$imagePathInfo = pathinfo($imagePath);
									$generatedThumbName = sprintf('%s_%s_%s.%s',
										md5('artsexythumb_' . $convertImage . '_' . $imagePath),
										$previewWidth,
										$previewHeight,
										$imagePathInfo['extension']);
									if (!file_exists(JPATH_SITE . DS . 'images' . DS . 'artsexylightbox_tmp')) {
										mkdir (JPATH_SITE . DS . 'images' . DS . 'artsexylightbox_tmp');
									}
									$thumbPath = JPATH_SITE . DS . 'images' . DS . 'artsexylightbox_tmp' . DS . $generatedThumbName;
									
									if (!@file_exists($thumbPath)) {
										$img = asido::image($imagePath, $thumbPath);
										if ($convertImage == 'crop' && $previewWidth && $previewHeight) {
											if ($cropStart) {
												$cropStartArray = explode(',', $cropStart);
												Asido::crop($img, $cropStartArray[0], $cropStartArray[1], $previewWidth, $previewHeight);
											  } else {
												Asido::crop($img, 0, 0, $previewWidth, $previewHeight);
											  }
										} else if ($convertImage == 'crop_resize' && $previewWidth && $previewHeight) {
											$imgSize = getimagesize($imagePath);
											$imgWidth = $imgSize[0];
											$imgHeight = $imgSize[1];
											if ($imgWidth > $imgHeight) {
												$diff = ($imgWidth - $imgHeight)/2;
												Asido::crop($img, $diff, 0, ($imgWidth - 2*$diff), $imgHeight);
											} else if ($imgWidth < $imgHeight) {
												$diff = ($imgHeight - $imgWidth)/2;
												Asido::crop($img, 0, $diff, $imgWidth, ($imgHeight - 2*$diff));
											}
											Asido::resize($img, $previewWidth, $previewHeight, ASIDO_RESIZE_STRETCH);
										} else if (strstr($convertImage, 'rotate')) {
											$rotateAttrs = explode(':', $convertImage);
											if (!$rotateAttrs[1]) {
												$rotateAttrs[1] = 90;
											}
											Asido::Rotate($img, $rotateAttrs[1]);
										} if ($convertImage == 'grayscale') {
											Asido::GreyScale($img);
										} else {
											if (!$previewWidth) {
												Asido::height($img, $previewHeight);
											} else if (!$previewHeight) {
												Asido::width($img, $previewWidth);
											} else {
												Asido::resize($img, $previewWidth, $previewHeight, ASIDO_RESIZE_STRETCH);
											}
										}
										$img->save(ASIDO_OVERWRITE_ENABLED);
									}
									$imagesCode .= JURI::root() . "images/artsexylightbox_tmp/$generatedThumbName' ";
								}
							} else {
								$imagesCode .= JURI::root() . "$path/$entry' ";
							}
							if ($previewHeight) {
								$imagesCode .= " height='$previewHeight' ";
							}
							if ($previewWidth) {
								$imagesCode .= "width='$previewWidth' ";
							}
							$imagesCode .= "/></a></li>";
						}
					}
					$imagesCount++;
				}
				if (isset($popup) && $popup == 'false') {
					return $imagesCode;
				}
				$jsCode .= 'var tabs = new Tabs("carousel-tabs' . $id . '", {idPrefix: "cs-"});tabs.startLoop(2000);})});</script>';
				$imagesCode .= '</ul></ul></div>';
				return $imagesCode.$jsCode;
			} else if ($fileList) {
      } else {
				/** Show gallery from local folder **/
				if ($subFolders) {
				  $imagesCode .= "<div class='dir_container'>";
				  $path1 = JRequest::getString('path1');
				  $articleid = JRequest::getString('id');
				  if ($articleid) {
					$imagesCode .= "<form method='post' id='dirform' action='' style='display:none'><input type='hidden' id='path1' name='path1' value='' /><input type='hidden' name='id' value='" . $articleid . "' /><input type='hidden' name='option' value='com_content' /></form>";
				  } else {
					$imagesCode .= "<form method='post' id='dirform' action='' style='display:none'><input type='hidden' id='path1' name='path1' value='' /></form>";
				  }
				
				  if ($path1) {
					$path = $path1;
					
					$filelist = array();
					$dh = @ opendir (JPATH_SITE . DS . str_replace('/', DS, $path1) . DS);
					if ($path1 != $finalParams['path']) {
					  $entry1 = substr($path, 0, strrpos($path, '/'));
					  $imagesCode .= '<div class="asl_folder"><a href="javascript:document.getElementById(\'path1\').value = \'' . $entry1 . '\';document.getElementById(\'dirform\').submit();" class="dir_link"><img src="' . JURI::root() . 'plugins/content/artsexylightbox/images/open.png" style="border:none" alt="folder" /><div>Back</div></a></div>';
					}
					while ($entry = readdir ($dh)) {
					  if (is_dir($path . DS . $entry) && $entry != '.' && $entry != '..') {
						$imagesCode .= '<div class="asl_folder"><a href="javascript:document.getElementById(\'path1\').value = \'' . $path . '/' . $entry . '\';document.getElementById(\'dirform\').submit();" class="dir_link"><img src="' . JURI::root() . 'plugins/content/artsexylightbox/images/folder.png" style="border:none" alt="folder" /><div>' . $entry . '</div></a></div>';
					  } else if ($entry != '.' && $entry != '..' && plgContentartsexylightbox::isImage($path . $entry)) {
						if ($sortBy == 'date') {
							  $filelist[filemtime(JPATH_SITE . DS . str_replace('/', DS, $path) . DS . $entry)] = $entry;
						  } else {
							  $filelist[] = $entry;
						  }
					  }
					  
					}
				  } else {
					$dh = @ opendir (JPATH_SITE . DS . $path . DS);
				  }
				  
				  if ($sortBy == 'date') {
					ksort($filelist);
					if ($sort == 'desc') {
					  $filelist = array_reverse($filelist);
					}
				  } else if ($sort == 'desc' && $sortBy == 'title') {
							usort ($filelist, 'artSLFileDescSort');
						} else if ($sort == 'asc' && $sortBy == 'title') {
							usort ($filelist, 'artSLFileAscSort');
						} else if ($sort == 'random') {
					  shuffle($filelist);
				  }
				
				  while (false !== ($filename = readdir($dh))) {
					if ($filename != '.' && $filename != '..') {
					  if (is_file($path . DS . $filename)) {
						//$filelist[] = $filename;
					  } elseif (is_dir($path . DS . $filename)) {
						$imagesCode .= '<div class="asl_folder"><a href="javascript:document.getElementById(\'path1\').value = \'' . $path . '/' . $filename . '\';document.getElementById(\'dirform\').submit();" class="dir_link"><img src="' . JURI::root() . 'plugins/content/artsexylightbox/images/folder.png" style="border:none" alt="folder" /><div>' . $filename . '</div></a></div>';
					  }
					}
				  }
				  $imagesCode .= "</div><div style='clear:both;'></div>";
				}
				if ($autoLoad) {
				  $jsCode .= 'SexyLightbox.hook("a.artsexylightboxpreview");';
				}
				$o = 0;
				while ((list ($key, $entry) = each ($filelist))) {
					if ($entry != '.' && $entry != '..' && plgContentartsexylightbox::isImage($path . $entry)) {
						$title = '';
						if (isset($descriptionArray[$entry])) {
							$title = $descriptionArray[$entry];
						}
						if ($caption) {
							$imagesCode .= "<div class='asl_image_caption'>";
						}
						if ($singleImage && $o == 0 && $showSingleImage != 'false') {
							$ind1 = strrpos($singleImage, '/');
							$sI = substr($singleImage, $ind1 + 1);
							if (isset($descriptionArray[$sI])) {
								$title = $descriptionArray[$sI];
							}
							$imagesCode .= "<a href='" . JURI::root() . "$entry";
							$imagesCode .= "' rel='sexylightbox[" . $id . "]' class='artsexylightboxpreview' ";
						} else {
							$imagesCode .= "<a href='" . JURI::root() . "$path/$entry";
							$imagesCode .= "' rel='sexylightbox[" . $id . "]' class='artsexylightboxpreview' ";
						}
						if ( ($numberOfImages && $numberOfImages > 0) && $o >= $numberOfImages) {
							$imagesCode .= " style='display:none;' ";
						}
						$o++;
						if (isset($title)) {
							$imagesCode .= " title='" . $title . "'";
						}
						if ($singleImage) {
								$imagesCode .= " style='display:none;'></a>";
						} else {
							$imagesCode .= ">";
							if ($pAlt) {
								$alt = $pAlt;
							} else if ($title) {
								$alt = $title;
							} else if ($path) {
								$ind = strrpos($path, '/');
								$alt = substr($path, $ind + 1);
							} else {
								$alt = 'artsexylightbox';
							}
							  if (!($thumbnailPreviewCount || $singleText)) {
								$imagesCode .= "<img alt='" . $alt . "' class='artsexylightbox' ";
								$imagesCode .= " src='"; 
								$imagePath = JPATH_SITE . DS . $path . DS . $entry;
								if ($thumbs && (isset($thumbs[$entry]) || isset($thumbs[(substr($thumbs[$entry], strrpos($thumbs[$entry], '.'))) . '.jpg']) && plgContentartsexylightbox::isFVL($entry))) {
								  $imagesCode .= JURI::root() . "$thumbnailPath/$entry";
								  $imagesCode .= "' ";
								} else if (@is_readable($imagePath) && ($previewWidth > 0 || $previewHeight > 0 || strstr($convertImage, 'rotate')) && isset($autoGenerateThumbs) && $autoGenerateThumbs != 'false') {
								  if (!plgContentartsexylightbox::isExtensionsLoaded()) {
                    plgContentartsexylightbox::loadExtensions();
								  }
								  if (!plgContentartsexylightbox::isExtensionsLoaded()) {
									$imagesCode .= JURI::root() . "$path/$entry' ";
								  } else {
									$imagePathInfo = pathinfo($imagePath);
									if ($watermark) {
									  $generatedThumbName = sprintf('%s_%s_%s.%s',
										md5('artsexythumb_' . $convertImage . '_' . $imagePath . '_' . $watermark . $watermarkPosition),
										$previewWidth,
										$previewHeight ,
										$imagePathInfo['extension']);
									} else {
									  $generatedThumbName = sprintf('%s_%s_%s.%s',
										md5('artsexythumb_' . $convertImage . '_' . $imagePath),
										$previewWidth,
										$previewHeight,
										$imagePathInfo['extension']);
									}
									if (!file_exists(JPATH_SITE . DS . 'images' . DS . 'artsexylightbox_tmp')) {
									  mkdir (JPATH_SITE . DS . 'images' . DS . 'artsexylightbox_tmp');
									}
									$thumbPath = JPATH_SITE . DS . 'images' . DS . 'artsexylightbox_tmp' . DS . $generatedThumbName;
									
                  
									if (!@file_exists($thumbPath)) {
									  $img = asido::image($imagePath, $thumbPath);
									  if ($convertImage == 'crop' && $previewWidth && $previewHeight) {
										if ($cropStart) {
										  $cropStartArray = explode(',', $cropStart);
										  Asido::crop($img, $cropStartArray[0], $cropStartArray[1], $previewWidth, $previewHeight);
										} else {
										  Asido::crop($img, 0, 0, $previewWidth, $previewHeight);
										}
									  } else if ($convertImage == 'crop_resize' && $previewWidth && $previewHeight) {
										$imgSize = getimagesize($imagePath);
										$imgWidth = $imgSize[0];
										$imgHeight = $imgSize[1];
										if ($imgWidth > $imgHeight) {
										  $diff = ($imgWidth - $imgHeight)/2;
										  Asido::crop($img, $diff, 0, ($imgWidth - 2*$diff), $imgHeight);
										} else if ($imgWidth < $imgHeight) {
										  $diff = ($imgHeight - $imgWidth)/2;
										  Asido::crop($img, 0, $diff, $imgWidth, ($imgHeight - 2*$diff));
										}
										Asido::resize($img, $previewWidth, $previewHeight, ASIDO_RESIZE_STRETCH);
									  } else if (strstr($convertImage, 'rotate')) {
                      $rotateAttrs = explode(':', $convertImage);
                      if (!$rotateAttrs[1]) {
                        $rotateAttrs[1] = 90;
                      }
                      Asido::Rotate($img, $rotateAttrs[1]);
									  } else {
										if (!$previewWidth) {
										  Asido::height($img, $previewHeight);
										} else if (!$previewHeight) {
										  Asido::width($img, $previewWidth);
										} else {
										  Asido::resize($img, $previewWidth, $previewHeight, ASIDO_RESIZE_STRETCH);
										}
                    if ($convertImage == 'grayscale') {
											Asido::GreyScale($img);
										}
									  }
									  if ($watermark) {
										switch($watermarkPosition) {
										  case 'topleft':
										  Asido::watermark($img, JPATH_SITE . DS . str_replace('/', DS, $watermark), ASIDO_WATERMARK_TOP_LEFT);
										  break;
										  
										  case 'topright':
										  Asido::watermark($img, JPATH_SITE . DS . str_replace('/', DS, $watermark), ASIDO_WATERMARK_TOP_RIGHT);
										  break;
										  
										  case 'bottomleft':
										  Asido::watermark($img, JPATH_SITE . DS . str_replace('/', DS, $watermark), ASIDO_WATERMARK_BOTTOM_LEFT);
										  break;
										  
										  case 'bottomright':
										  Asido::watermark($img, JPATH_SITE . DS . str_replace('/', DS, $watermark), ASIDO_WATERMARK_BOTTOM_RIGHT);
										  break;
										  
										  default:
										  Asido::watermark($img, JPATH_SITE . DS . str_replace('/', DS, $watermark), ASIDO_WATERMARK_TOP_LEFT);
										  break;
										}
									  }
									  $img->save(ASIDO_OVERWRITE_ENABLED);
									}
									$imagesCode .= JURI::root() . "images/artsexylightbox_tmp/$generatedThumbName' ";
								  }
								} else {
								  $imagesCode .= JURI::root() . "$path/$entry' ";
								}
								if ($previewHeight) {
								  $imagesCode .= " height='$previewHeight' ";
								}
								if ($previewWidth) {
								  $imagesCode .= "width='$previewWidth' ";
								}
								$imagesCode .= "/>";
							  }
							$imagesCode .= "</a>";
							if ($caption) {
								$title = str_replace('&lt;', '<', $title);
								$title = str_replace('&gt;', '>', $title);
								$imagesCode .= '<div class="asl_caption">' . html_entity_decode($title) . '</div>';
								$imagesCode .= '</div>';
							}
						}
					}
					$imagesCount++;
				}
				if (isset($popup) && $popup == 'false') {
					return $imagesCode;
				}
				$jsCode .= '})});</script>';
				$imagesCode .= '</div>';
				return $imagesCode.$jsCode;
			}
		/** Show Flickr images **/
		} else if (isset($flickrImage)) {
      if ($fp = fopen('http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=' . $flickrApiKey . '&photo_id=' . $flickrImage . '&format=php_serial', 'r')) {
         $content = '';
         while ($line = fread($fp, 1024)) {
          $content .= $line;
         }
         $rsp_obj = unserialize($content);
         
         $image = $rsp_obj['sizes']['size'];
         if ($flickrImageSize == 's') {
          $image = $image[0];
         } else if ($flickrImageSize == 't') {
          $image = $image[1];
         } else if ($flickrImageSize == 'm') {
          $image = $image[2];
         } else {
          $image = $image[3];
         }
         $image = $image['source'];
         
         $thumb = $rsp_obj['sizes']['size'];
         if ($flickrThumbSize == 's') {
          $thumb = $thumb[0];
         } else if ($flickrThumbSize == 't') {
          $thumb = $thumb[1];
         } else if ($flickrThumbSize == 'm') {
          $thumb = $thumb[2];
         } else {
          $thumb = $thumb[0];
         }
         $thumb = $thumb['source'];
         
          $jsCode .= 'if (!window.sexylightboxEnabled) {SexyLightbox.initialize(' . $dFP . ');} if (!window.sexylightboxEnabled) {window.sexylightboxEnabled = true;}';
          if ($autoLoad) {
            $jsCode .= 'SexyLightbox.hook("a.artsexylightboxpreview");';
          }
          if ($autoClose) {
            $jsCode .= 'window.setTimeout(function(){SexyLightbox.close();}, ' . $autoClose . ')';
          }
          $imagesCode .= "<a class='artsexylightboxpreview' rel='sexylightbox' href='" . $image;
          $imagesCode .= "'";
          if ($title) {
            $imagesCode .= " title='" . $title . "'";
          } else {
            $imagesCode .= " title=' '";
          }
          $imagesCode .= ">";
          $imagesCode .= "<img alt='$pAlt' class='artsexylightbox_singleimage' src='" . $thumb . "'";
          $imagesCode .= " /></a>";
          $jsCode .= '})});</script>';
          $imagesCode .= '</div>';
          return $imagesCode.$jsCode;
      }
    } else if ($flickr) {
			if (!$singleImage) {
				$singleImage = false;
			}
			/** Show Flickr photo set **/
			if (isset($flickrApiKey)) {
        if (isset($flickrSet)) {
				if ($carousel) {
          $imagesCode .= '<div class="asl_prev" id="aslcarouselprev_' . $id . '"></div><div class="asl_carousel" id="aslcarousel_' . $id . '"><ul id="' . $id . '" class="artflickrgallery">';
          if (isset($popup) && $popup == 'false') {
            $jsCode .= 'asljQuery("#' . $id . '").flickr({page: "' . $page . '", carousel: true, singleImage: "' . $singleImage . '", find: "' . $id . '", thumb_size:"' . $flickrThumbSize . '",size:"' . $flickrImageSize . '",per_page:' . $flickrNumberOfImages . ',api_key:"' . $flickrApiKey . '",photoset_id:"' . $flickrSet . '", user_id: "' . $flickr . '", type: "photoset",callback: function(el) {}});';
          } else {
            $jsCode .= 'asljQuery("#' . $id . '").flickr({page: "' . $page . '", carousel: true, singleImage: "' . $singleImage . '", find: "' . $id . '", thumb_size:"' . $flickrThumbSize . '",size:"' . $flickrImageSize . '",per_page:' . $flickrNumberOfImages . ',api_key:"' . $flickrApiKey . '",photoset_id:"' . $flickrSet . '", user_id: "' . $flickr . '", type: "photoset",callback: function(el) {SexyLightbox.initialize(' . $dFP . ');asljQuery(function() {asljQuery("#aslcarousel_' . $id . '").jCarouselLite({vertical: ' . $vertical . ', circular: false, visible: ' . $carousel_visible . ', scroll: ' . $carousel_scroll . ', btnNext: "#aslcarouselnext_' . $id . '", btnPrev: "#aslcarouselprev_' . $id . '"});});}});';
          }
          $imagesCode .= '</ul></div><div id="aslcarouselnext_' . $id . '" class="asl_next"></div><div style="clear:both"></div>';
          $imagesCode .= '</div>';
        } else {
          $imagesCode .= "<div id='$id' class='artflickrgallery'></div>";
          if (isset($popup) && $popup == 'false') {
            $jsCode .= 'asljQuery("#' . $id . '").flickr({page: "' . $page . '", singleImage: "' . $singleImage . '", find: "' . $id . '", thumb_size:"' . $flickrThumbSize . '",size:"' . $flickrImageSize . '",per_page:' . $flickrNumberOfImages . ',api_key:"' . $flickrApiKey . '",photoset_id:"' . $flickrSet . '", user_id: "' . $flickr . '", type: "photoset",callback: function(el) {}});';
          } else {
            $jsCode .= 'asljQuery("#' . $id . '").flickr({page: "' . $page . '", singleImage: "' . $singleImage . '", find: "' . $id . '", thumb_size:"' . $flickrThumbSize . '",size:"' . $flickrImageSize . '",per_page:' . $flickrNumberOfImages . ',api_key:"' . $flickrApiKey . '",photoset_id:"' . $flickrSet . '", user_id: "' . $flickr . '", type: "photoset",callback: function(el) {SexyLightbox.initialize(' . $dFP . ');}});';
          }
        }
        } else {
          $photosetid = JRequest::getString('photosetid');
          $articleid = JRequest::getString('id');
          if (!isset($photosetid) || $photosetid == 0) {
            $j6 = 'artsexylightbox/';
            if (file_exists(JPATH_SITE . DS . 'plugins' . DS . 'content' . DS . 'artsexylightbox' . DS . 'library' . DS . 'asido' . DS . 'class.asido.php')) {
              $j6 = '';
            }
            require_once(JPATH_SITE . DS . 'plugins' . DS . 'content' . DS . 'artsexylightbox' . DS . $j6 . DS . 'library' . DS . 'phpFlickr.php');
            $f = new phpFlickr($flickrApiKey);
            $user = $flickr;
            $ph_sets = $f->photosets_getList($user);
            $hh = 1;
            $imagesCode .= "<form id='flickrform' action='' style='display:none'><input type='hidden' id='photosetid' name='photosetid' value='' /><input type='hidden' name='id' value='" . $articleid . "' /><input type='hidden' name='option' value='com_content' /></form>";
            foreach ($ph_sets['photoset'] as $ph_set) {
              $photoset_id = $ph_set['id'];
              $imagesCode .= "<div class='gallerycontainer'><div class='gallerytitle'>" . $ph_set['title']. "</div><div id='artflickrgallery" . '_' . $hh . "' class='artflickrgallery'></div></div>";
              if (isset($popup) && $popup == 'false') {
                $jsCode .= 'asljQuery("#artflickrgallery_' . $hh . '").flickr({page: "' . $page . '", singleImage: "' . $singleImage . '", find: "' . $id . '", thumb_size:"' . $flickrThumbSize . '",size:"' . $flickrImageSize . '",per_page:' . $flickrNumberOfImages . ',api_key:"' . $flickrApiKey . '",photoset_id:"' . $photoset_id . '", user_id: "' . $flickr . '", type: "photoset",callback: function(el) {}});';
              } else {
                $jsCode .= 'asljQuery("#artflickrgallery_' . $hh . '").flickr({page: "' . $page . '", singleImage: "' . $singleImage . '", find: "' . $id . '", thumb_size:"' . $flickrThumbSize . '",size:"' . $flickrImageSize . '",per_page:1,api_key:"' . $flickrApiKey . '",photoset_id:"' . $photoset_id . '", user_id: "' . $flickr . '", type: "photoset",callback: function(el) {asljQuery("#artflickrgallery_' . $hh . ' a").click(function(event){event.preventDefault();document.getElementById("photosetid").value="' . $photoset_id . '";document.getElementById("flickrform").submit();});}});';
              }
              $hh++;
            }
          } else {
            $imagesCode .= "<div id='artflickrgallery" . '_' . $hh . "' class='artflickrgallery'></div>";
            if (isset($popup) && $popup == 'false') {
              $jsCode .= 'asljQuery("#artflickrgallery_' . $hh . '").flickr({page: "' . $page . '", singleImage: "' . $singleImage . '", find: "' . $id . '", thumb_size:"' . $flickrThumbSize . '",size:"' . $flickrImageSize . '",per_page:' . $flickrNumberOfImages . ',api_key:"' . $flickrApiKey . '",photoset_id:"' . $photosetid . '", user_id: "' . $flickr . '", type: "photoset",callback: function(el) {}});';
            } else {
              $jsCode .= 'asljQuery("#artflickrgallery_' . $hh . '").flickr({page: "' . $page . '", singleImage: "' . $singleImage . '", find: "' . $id . '", thumb_size:"' . $flickrThumbSize . '",size:"' . $flickrImageSize . '",per_page:' . $flickrNumberOfImages . ',api_key:"' . $flickrApiKey . '",photoset_id:"' . $photosetid . '", user_id: "' . $flickr . '", type: "photoset",callback: function(el) {SexyLightbox.initialize(' . $dFP . ');}});';
            }
          }
        }
        
			/** Show Flickr photo feed **/
			} else {
        if ($carousel) {
          if (strstr($flickr, ',')) {
            $ids = explode(',', $flickr);
          } else {
            $id = $flickr;
          }
          if ($id) {
            $url = 'http://api.flickr.com/services/feeds/photos_public.gne?format=php_serial&id=' . $id;
          } else {
            $url = 'http://api.flickr.com/services/feeds/photos_public.gne?format=php_serial&ids=' . $ids;
          }
          $id = uniqid('artgallery_', false);
          $imagesCode .= '<div class="asl_prev" id="aslcarouselprev_' . $id . '"></div><div class="asl_carousel" id="aslcarousel_' . $id . '"><ul>';
          $jsCode .= 'if (!window.sexylightboxEnabled) {SexyLightbox.initialize(' . $dFP . ');} if (!window.sexylightboxEnabled) {window.sexylightboxEnabled = true;}';
          ini_set('allow_url_fopen', 1);
          if ($fp = fopen($url, 'r')) {
             $content = '';
             while ($line = fread($fp, 1024)) {
              $content .= $line;
             }
          } else {
            $ch = curl_init();
                  curl_setopt($ch, CURLOPT_URL, $url);
                  curl_setopt($ch, CURLOPT_HEADER, 0);
                  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                  $content = curl_exec($ch);
          }
          if ($content) {
             $rsp_obj = unserialize($content);
             
             $i = 1;
             
             if ($singleImage && $singleImage !== 'true') {
              $imagesCode .= "<li><a href='" . substr($singleImage, 0, -6) . ".jpg' rel='sexylightbox[" . $id . "]' class='artsexylightboxpreview' ";
              $imagesCode .= "><img alt='$pAlt' class='artsexylightbox' src='" . $singleImage . "'";
              if ($previewHeight) {
                $imagesCode .= " height='$previewHeight' ";
              }
              if ($previewWidth) {
                $imagesCode .= "width='$previewWidth' ";
              }
              $imagesCode .= "/></a></li>";
            }

             foreach ($rsp_obj['items'] as $image) {
              if ($i > $flickrNumberOfImages) {
                break;
              }
              $title = htmlspecialchars($image['title'], ENT_QUOTES);
              $largeImagePath = $image['m_url'];
              $smallThumbPath = $image['m_url'];
              if ($flickrLargeImages) {
                $largeImagePath = $image['l_url'];
              }
              if ($flickrSmallThumbs) {
                $smallThumbPath = $image['t_url'];
              }
              
              $imagesCode .= "<li><a href='" . $largeImagePath . "' rel='sexylightbox[" . $id . "]' class='artsexylightboxpreview' ";
              if ($title) {
                $imagesCode .= "title='" . $title . "'";
              }
              if ($singleImage) {
                $imagesCode .= " style='display:none;'";
              }
              $imagesCode .= "><img alt='$pAlt' class='artsexylightbox' src='" . $smallThumbPath . "'";
              if ($previewHeight) {
                $imagesCode .= " height='$previewHeight' ";
              }
              if ($previewWidth) {
                $imagesCode .= "width='$previewWidth' ";
              }
              $imagesCode .= "/></a></li>";
              $i++;
            }
          }
          $imagesCode .= '</ul></div><div id="aslcarouselnext_' . $id . '" class="asl_next"></div><div style="clear:both"></div>';
          $jsCode .= 'asljQuery(function() {asljQuery("#aslcarousel_' . $id . '").jCarouselLite({vertical: ' . $vertical . ', circular: false, visible: ' . $carousel_visible . ', scroll: ' . $carousel_scroll . ', btnNext: "#aslcarouselnext_' . $id . '", btnPrev: "#aslcarouselprev_' . $id . '"';
          if ($auto) {
            $jsCode .= ',auto:' . $auto;
          }
          $jsCode .= '});});';
        } else if ($cloudCarousel) {
          if (strstr($flickr, ',')) {
            $ids = explode(',', $flickr);
          } else {
            $idD = $flickr;
          }
          if ($idD) {
            $url = 'http://api.flickr.com/services/feeds/photos_public.gne?format=php_serial&id=' . $idD;
          } else {
            $url = 'http://api.flickr.com/services/feeds/photos_public.gne?format=php_serial&ids=' . $ids;
          }
          
          $finalParams['buttonLeft'] = '#carousel_left' . $id;
          $finalParams['buttonRight'] = '#carousel_right' . $id;
          
          $jsCode .= 'if (!window.sexylightboxEnabled) {SexyLightbox.initialize(' . $dFP . ');} if (!window.sexylightboxEnabled) {window.sexylightboxEnabled = true;}';
          ini_set('allow_url_fopen', 1);
          if ($fp = fopen($url, 'r')) {
             $content = '';
             while ($line = fread($fp, 1024)) {
              $content .= $line;
             }
          } else {
            $ch = curl_init();
                  curl_setopt($ch, CURLOPT_URL, $url);
                  curl_setopt($ch, CURLOPT_HEADER, 0);
                  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                  $content = curl_exec($ch);
          }
          if ($content) {
             $rsp_obj = unserialize($content);
             
             $i = 1;
             
             if ($singleImage && $singleImage !== 'true') {
              $imagesCode .= "<a href='" . substr($singleImage, 0, -6) . ".jpg' rel='sexylightbox[" . $id . "]' class='artsexylightboxpreview' ";
              $imagesCode .= "><img alt='$pAlt' class='artsexylightbox' src='" . $singleImage . "'";
              if ($previewHeight) {
                $imagesCode .= " height='$previewHeight' ";
              }
              if ($previewWidth) {
                $imagesCode .= "width='$previewWidth' ";
              }
              $imagesCode .= "/></a>";
            }

             foreach ($rsp_obj['items'] as $image) {
              if ($i > $flickrNumberOfImages) {
                break;
              }
              $title = htmlspecialchars($image['title'], ENT_QUOTES);
              $largeImagePath = $image['m_url'];
              $smallThumbPath = $image['m_url'];
              if ($flickrLargeImages) {
                $largeImagePath = $image['l_url'];
              }
              if ($flickrSmallThumbs) {
                $smallThumbPath = $image['t_url'];
              }
              
              $imagesCode .= "<a href='" . $largeImagePath . "' rel='sexylightbox[" . $id . "]' class='artsexylightboxpreview' ";
              if ($title) {
                $imagesCode .= "title='" . $title . "'";
              }
              if ($singleImage) {
                $imagesCode .= " style='display:none;'";
              }
              $imagesCode .= "><img alt='$pAlt' class='artsexylightbox cloudcarousel' src='" . $smallThumbPath . "'";
              if ($previewHeight) {
                $imagesCode .= " height='$previewHeight' ";
              }
              if ($previewWidth) {
                $imagesCode .= "width='$previewWidth' ";
              }
              $imagesCode .= "/></a>";
              $i++;
            }
          }
          $jsCode .= 'asljQuery(".artsexylightbox_container").CloudCarousel(' . $dFP . ');})});</script>';
          $imagesCode .= '<div class="cloud_carousel_title"></div>';
          if (!$hideButtons) {
            $imagesCode .= '<div id="carousel_left' . $id . '" class="carouselLeft"></div>';
            $imagesCode .= '<div id="carousel_right' . $id . '" class="carouselRight"></div>';
          }
          $imagesCode .= '</div>';
          return $imagesCode.$jsCode;
        } else {
          $jsCode .= 'if (!window.sexylightboxEnabled) {SexyLightbox.initialize(' . $dFP . ');} if (!window.sexylightboxEnabled) {window.sexylightboxEnabled = true;}';
          if (strstr($flickr, ',')) {
            $ids = explode(',', $flickr);
          } else {
            $id = $flickr;
          }
          if ($id) {
            $url = 'http://api.flickr.com/services/feeds/photos_public.gne?format=php_serial&id=' . $id;
          } else {
            $url = 'http://api.flickr.com/services/feeds/photos_public.gne?format=php_serial&ids=' . $ids;
          }
          ini_set('allow_url_fopen', 1);
          if ($fp = fopen($url, 'r')) {
             $content = '';
             while ($line = fread($fp, 1024)) {
              $content .= $line;
             }
          } else {
            $ch = curl_init();
                  curl_setopt($ch, CURLOPT_URL, $url);
                  curl_setopt($ch, CURLOPT_HEADER, 0);
                  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                  $content = curl_exec($ch);
          }
          if ($content) {
             $rsp_obj = unserialize($content);
             
             $i = 1;
             
             if ($singleImage && $singleImage !== 'true') {
              $imagesCode .= "<a href='" . substr($singleImage, 0, -6) . ".jpg' rel='sexylightbox[" . $id . "]' class='artsexylightboxpreview' ";
              $imagesCode .= "><img alt='$pAlt' class='artsexylightbox' src='" . $singleImage . "'";
              if ($previewHeight) {
                $imagesCode .= " height='$previewHeight' ";
              }
              if ($previewWidth) {
                $imagesCode .= "width='$previewWidth' ";
              }
              $imagesCode .= "/></a>";
            }

             foreach ($rsp_obj['items'] as $image) {
              if ($i > $flickrNumberOfImages) {
                break;
              }
              $title = htmlspecialchars($image['description_raw'], ENT_QUOTES);
              $largeImagePath = $image['m_url'];
              $smallThumbPath = $image['m_url'];
              if ($flickrLargeImages) {
                $largeImagePath = $image['photo_url'];
              }
              if ($flickrSmallThumbs) {
                $smallThumbPath = $image['t_url'];
              }
              
              $imagesCode .= "<a href='" . $largeImagePath . "' rel='sexylightbox[" . $id . "]' class='artsexylightboxpreview' ";
              if ($title) {
                $imagesCode .= "title='" . $title . "'";
              }
              if ($singleImage) {
                $imagesCode .= " style='display:none;'";
              }
              $imagesCode .= "><img alt='$pAlt' class='artsexylightbox' src='" . $smallThumbPath . "'";
              if ($previewHeight) {
                $imagesCode .= " height='$previewHeight' ";
              }
              if ($previewWidth) {
                $imagesCode .= "width='$previewWidth' ";
              }
              $imagesCode .= "/></a>";
              $i++;
            }
          }
				}
			}
			$imagesCode .= '</div>';
			$jsCode .= '})});</script>';
			return $imagesCode.$jsCode;
		/** Show picasa photos **/
		} else if ($picasaUser) {
			if ($carousel) {
				$jsCode .= 'if (!window.sexylightboxEnabled) {SexyLightbox.initialize(' . $dFP . ');} if (!window.sexylightboxEnabled) {window.sexylightboxEnabled = true;}';
				$imagesCode .= '<div class="asl_prev" id="aslcarouselprev_' . $id . '"></div><div class="asl_carousel" id="aslcarousel_' . $id . '"><ul>';
				$feedURL = "http://picasaweb.google.com/data/feed/api/user/$picasaUser/album/$picasaAlbum";
				ini_set('allow_url_fopen', 1);
				$sxml = simplexml_load_file($feedURL);
				if (!$sxml) {
					$string= file_get_contents($feedURL);
					$ch = curl_init();
					curl_setopt($ch, CURLOPT_URL, $feedURL);
					curl_setopt($ch, CURLOPT_HEADER, 0);
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
					$content = curl_exec($ch);
					$sxml = simplexml_load_string($content);
				}
				if (!$sxml || !$sxml->children('http://a9.com/-/spec/opensearchrss/1.0/')) {
					$imagesCode = 'Picasa user or album is not correct';
				} else {
					$counts = $sxml->children('http://a9.com/-/spec/opensearchrss/1.0/');
					$total = $counts->totalResults;
					$k = 0;
					$i = 1;
					foreach ($sxml->entry as $entry) {
						if ( ($numberOfImages && $numberOfImages > 0) && $i > $numberOfImages) {
							break;
						}
						$summary = $entry->summary;
						$title = htmlspecialchars($summary, ENT_QUOTES);

						$gphoto = $entry->children('http://schemas.google.com/photos/2007');
						
						$size = $gphoto->size;
						$height = $gphoto->height;
						$width = $gphoto->width;

						$media = $entry->children('http://search.yahoo.com/mrss/');
						
						if ($picasaImageSize && $picasaImageSize == 'tiny') {
						  $thumbnail = $media->group->thumbnail[0];
						} else if ($picasaImageSize && $picasaImageSize == 'small') {
						  $thumbnail = $media->group->thumbnail[1];
						} else if ($picasaImageSize && $picasaImageSize == 'big') {
						  $thumbnail = $media->group->thumbnail[2];
						} else {
						  $thumbnail = $media->group->thumbnail[1];
						}

						
						$pContent = $entry->content;
						
						$tags = $media->group->keywords;
						
						$attr = $pContent->attributes();
						$tattr = $thumbnail->attributes();
						$imgSrc = $attr->{'src'};
            $imagesCode .= "<li><a href='" . substr($imgSrc, 0, strripos ($imgSrc, '/')) . '/s' . $picasaLargeImageSize . substr($imgSrc, strripos ($imgSrc, '/')) . "' rel='sexylightbox[" . $id . "]' class='artsexylightboxpreview' ";
						if ($title) {
							$imagesCode .= "title='" . $title . "'";
						}
						if ($singleImage && $k != 0) {
							$imagesCode .= " style='display:none;'";
						}
						$imagesCode .= "><img alt='$title' class='artsexylightbox' src='" . $tattr->{'url'} . "'";
						if ($previewHeight) {
							$imagesCode .= " height='$previewHeight' ";
						}
						if ($previewWidth) {
							$imagesCode .= "width='$previewWidth' ";
						}
						$imagesCode .= "/></a>";
						$k++;
						$i++;
					}
				}
				$imagesCode .= '</ul></div><div id="aslcarouselnext_' . $id . '" class="asl_next"></div><div style="clear:both"></div>';
				$jsCode .= '})});asljQuery(window).load(function() {asljQuery("#aslcarousel_' . $id . '").jCarouselLite({vertical: ' . $vertical . ', circular: false ';
				if ($auto) {
				  $jsCode .= ',auto:' . $auto . ',speed:' . $speed . '';
				}
				$jsCode .= ',visible: ' . $carousel_visible . ', scroll: ' . $carousel_scroll . ', btnNext: "#aslcarouselnext_' . $id . '", btnPrev: "#aslcarouselprev_' . $id . '"});});</script>';
				$imagesCode .= '</div>';
				if (isset($popup) && $popup == 'false') {
					$jsCode = '<script type="text/javascript" charset="utf-8">asljQuery(function() {asljQuery("#aslcarousel_' . $id . '").jCarouselLite({vertical: ' . $vertical . ', circular: false, visible: ' . $carousel_visible . ', scroll: ' . $carousel_scroll . ', btnNext: "#aslcarouselnext_' . $id . '", btnPrev: "#aslcarouselprev_' . $id . '"});});</script>';
					return $imagesCode.$jsCode;
				}
				return $imagesCode.$jsCode;
			} else if ($cloudCarousel) {
				$jsCode .= 'if (!window.sexylightboxEnabled) {SexyLightbox.initialize(' . $dFP . ');} if (!window.sexylightboxEnabled) {window.sexylightboxEnabled = true;}';
				$finalParams['buttonLeft'] = '#carousel_left' . $id;
				$finalParams['buttonRight'] = '#carousel_right' . $id;
				$feedURL = "http://picasaweb.google.com/data/feed/api/user/$picasaUser/album/$picasaAlbum";
				ini_set('allow_url_fopen', 1);
				$sxml = simplexml_load_file($feedURL);
				if (!$sxml) {
					$string= file_get_contents($feedURL);
					$ch = curl_init();
					curl_setopt($ch, CURLOPT_URL, $feedURL);
					curl_setopt($ch, CURLOPT_HEADER, 0);
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
					$content = curl_exec($ch);
					$sxml = simplexml_load_string($content);
				}
				if (!$sxml || !$sxml->children('http://a9.com/-/spec/opensearchrss/1.0/')) {
					$imagesCode = 'Picasa user or album is not correct';
				} else {
					$counts = $sxml->children('http://a9.com/-/spec/opensearchrss/1.0/');
					$total = $counts->totalResults;
					$k = 0;
					$i = 1;
					foreach ($sxml->entry as $entry) {
						if ( ($numberOfImages && $numberOfImages > 0) && $i > $numberOfImages) {
							break;
						}
						$summary = $entry->summary;
						$title = htmlspecialchars($summary, ENT_QUOTES);

						$gphoto = $entry->children('http://schemas.google.com/photos/2007');
						
						$size = $gphoto->size;
						$height = $gphoto->height;
						$width = $gphoto->width;

						$media = $entry->children('http://search.yahoo.com/mrss/');
						
						if ($picasaImageSize && $picasaImageSize == 'tiny') {
						  $thumbnail = $media->group->thumbnail[0];
						} else if ($picasaImageSize && $picasaImageSize == 'small') {
						  $thumbnail = $media->group->thumbnail[1];
						} else if ($picasaImageSize && $picasaImageSize == 'big') {
						  $thumbnail = $media->group->thumbnail[2];
						} else {
						  $thumbnail = $media->group->thumbnail[1];
						}

						$pContent = $entry->content;
						
						$tags = $media->group->keywords;
						
						$attr = $pContent->attributes();
						$tattr = $thumbnail->attributes();
						$imgSrc = $attr->{'src'};
						$imagesCode .= "<a href='" . substr($imgSrc, 0, strripos ($imgSrc, '/')) . '/s' . $picasaLargeImageSize . substr($imgSrc, strripos ($imgSrc, '/')) . "' rel='sexylightbox[" . $id . "]' class='artsexylightboxpreview' ";
						if ($title) {
							$imagesCode .= "title='" . $title . "'";
						}
						if ($singleImage && $k != 0) {
							$imagesCode .= " style='display:none;'";
						}
						$imagesCode .= "><img alt='$title' class='artsexylightbox cloudcarousel' src='" . $tattr->{'url'} . "'";
						if ($previewHeight) {
							$imagesCode .= " height='$previewHeight' ";
						}
						if ($previewWidth) {
							$imagesCode .= "width='$previewWidth' ";
						}
						$imagesCode .= "/></a>";
						$k++;
						$i++;
					}
				}
				  $jsCode .= 'asljQuery(".artsexylightbox_container").CloudCarousel(' . $dFP . ');})});</script>';
				  $imagesCode .= '<div class="cloud_carousel_title"></div>';
				  if (!$hideButtons) {
					$imagesCode .= '<div id="carousel_left' . $id . '" class="carouselLeft"></div>';
					$imagesCode .= '<div id="carousel_right' . $id . '" class="carouselRight"></div>';
				  }
				  $imagesCode .= '</div>';
				  return $imagesCode.$jsCode;
			  } else {
				if ($picasaAlbum) {
				$jsCode .= 'if (!window.sexylightboxEnabled) {SexyLightbox.initialize(' . $dFP . ');} if (!window.sexylightboxEnabled) {window.sexylightboxEnabled = true;}';
				$feedURL = "http://picasaweb.google.com/data/feed/api/user/$picasaUser/album/$picasaAlbum";
				ini_set('allow_url_fopen', 1);
				$sxml = simplexml_load_file($feedURL);
				if (!$sxml) {
					$string= file_get_contents($feedURL);
					$ch = curl_init();
					curl_setopt($ch, CURLOPT_URL, $feedURL);
					curl_setopt($ch, CURLOPT_HEADER, 0);
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
					$content = curl_exec($ch);
					$sxml = simplexml_load_string($content);
				}
				if (!$sxml || !$sxml->children('http://a9.com/-/spec/opensearchrss/1.0/')) {
					$imagesCode = 'Picasa user or album is not correct';
				} else {
					$counts = $sxml->children('http://a9.com/-/spec/opensearchrss/1.0/');
					$total = $counts->totalResults;
					$k = 0;
					$i = 0;
					  if ($singleImage && $singleImage !== 'true') {
						$imgSrc = $attr->{'src'};
						$imagesCode .= "<a href='" . substr($imgSrc, 0, strripos ($imgSrc, '/')) . '/s' . $picasaLargeImageSize . substr($imgSrc, strripos ($imgSrc, '/')) . "' rel='sexylightbox[" . $id . "]' class='artsexylightboxpreview' ";
						$imagesCode .= " onclick='asljQuery(\"a[rel^=sexylightbox[" . $id . "]]\").eq(0).click();return false;'";
						$imagesCode .= "><img src='" . JURI::root() . $singleImage . "' class='artsexylightbox'";
						if ($previewWidth) {
						  $imagesCode .= " width='" . $previewWidth . "'";
						}
						if ($previewHeight) {
						  $imagesCode .= " height='" . $previewHeight . "'";
						}
						$imagesCode .= "></a>";
					  } else if ($singleText) {
						$imgSrc = $attr->{'src'};
						$imagesCode .= "<a href='" . substr($imgSrc, 0, strripos ($imgSrc, '/')) . '/s' . $picasaLargeImageSize . substr($imgSrc, strripos ($imgSrc, '/')) . "' rel='sexylightbox[" . $id . "]' class='artsexylightboxpreview' ";
						$imagesCode .= " onclick='asljQuery(\"a[rel^=sexylightbox[" . $id . "]]\").eq(0).click();return false;'";
						$imagesCode .= ">" . $singleText;
						$imagesCode .= "</a>";
					  }
					  $filelist = array();
					  foreach ($sxml->entry as $entry) {
						$filelist[] = $entry;
					  }
					  if ($sort == 'random') {
						  shuffle($filelist);
					  }
					foreach ($filelist as $entry) {
						$summary = $entry->summary;
						$title = htmlspecialchars($summary, ENT_QUOTES);

						$gphoto = $entry->children('http://schemas.google.com/photos/2007');
						
						$size = $gphoto->size;
						$height = $gphoto->height;
						$width = $gphoto->width;

						$media = $entry->children('http://search.yahoo.com/mrss/');
						
						if ($picasaImageSize && $picasaImageSize == 'tiny') {
						  $thumbnail = $media->group->thumbnail[0];
						} else if ($picasaImageSize && $picasaImageSize == 'small') {
						  $thumbnail = $media->group->thumbnail[1];
						} else if ($picasaImageSize && $picasaImageSize == 'big') {
						  $thumbnail = $media->group->thumbnail[2];
						} else {
						  $thumbnail = $media->group->thumbnail[1];
						}

						
						$pContent = $entry->content;
						
						$tags = $media->group->keywords;
						
						$attr = $pContent->attributes();
						$tattr = $thumbnail->attributes();
						if ($caption) {
							$imagesCode .= "<div class='asl_image_caption'>";
						}
						$imgSrc = urldecode($attr->{'src'});
						$imagesCode .= "<a href='" . substr($imgSrc, 0, strripos ($imgSrc, '/')) . '/s' . $picasaLargeImageSize . substr($imgSrc, strripos ($imgSrc, '/')) . "' rel='sexylightbox[" . $id . "]' class='artsexylightboxpreview' ";
						if ( ($numberOfImages && $numberOfImages > 0) && $i > $numberOfImages) {
							$imagesCode .= " style='display:none;' ";
						}
						if ($title) {
						$title = str_replace('$', "&#036;", $title);
							$imagesCode .= "title='" . $title . "'";
						}
						if ($singleImage) {
						  if ($singleImage === 'true') {
							if ($k != 0) {
							  $imagesCode .= " style='display:none;'";
							}
						  } else {
							$imagesCode .= " style='display:none;'";
						  }
						} else if ($singleText) {
							$imagesCode .= " style='display:none;'";
						}
						$imagesCode .= "><img alt='$title' class='artsexylightbox' src='" . urldecode($tattr->{'url'}) . "'";
						if ($previewHeight) {
							$imagesCode .= " height='$previewHeight' ";
						}
						if ($previewWidth) {
							$imagesCode .= "width='$previewWidth' ";
						}
						$imagesCode .= "/></a>";
						if ($caption) {
							$title = str_replace('&lt;', '<', $title);
							$title = str_replace('&gt;', '>', $title);
							$imagesCode .= '<div class="asl_caption">' . html_entity_decode($title) . '</div>';
							$imagesCode .= '</div>';
						}
						$k++;
						$i++;
					}
				}
				$imagesCode .= '</div>';
				if (isset($popup) && $popup == 'false') {
					return $imagesCode;
				}
				$jsCode .= '})});</script>';
				return $imagesCode.$jsCode;
        } else {
          $albumId = JRequest::getString('albumid');
          if (!$albumId) {
            $feedURL = "http://picasaweb.google.com/data/feed/api/user/$picasaUser?kind=album";
            ini_set('allow_url_fopen', 1);
            $sxml = simplexml_load_file($feedURL);
            if (!$sxml) {
              $string= file_get_contents($feedURL);
              $ch = curl_init();
              curl_setopt($ch, CURLOPT_URL, $feedURL);
              curl_setopt($ch, CURLOPT_HEADER, 0);
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
              $content = curl_exec($ch);
              $sxml = simplexml_load_string($content);
            }
            if (!$sxml || !$sxml->children('http://a9.com/-/spec/opensearchrss/1.0/')) {
              $imagesCode = 'Picasa user or album is not correct';
            } else {
              $counts = $sxml->children('http://a9.com/-/spec/opensearchrss/1.0/');
              $total = $counts->totalResults;
              $k = 0;
              $i = 1;
              $articleid = JRequest::getString('id');
              foreach ($sxml->entry as $entry) {
                $gphoto = $entry->children('http://schemas.google.com/photos/2007');
                $title = $entry->title;
                $albumId = $gphoto->name;
                $albumId = str_replace('-', '', $albumId);
                $albumId = str_replace(' ', '', $albumId);
                $imagesCode .= "<form id='picasaform" . $i . "' action='' style='display:none'><input type='hidden' name='albumid' value='" . $albumId . "' /><input type='hidden' name='id' value='" . $articleid . "' /><input type='hidden' name='option' value='com_content' /><input type='hidden' name='view' value='article' /></form>";
                $imagesCode .= "<div class='gallerycontainer'><div class='gallerytitle'><a href='#' onclick='document.getElementById(\"picasaform" . $i . "\").submit();return false;'>" . $title. "</a></div></div>";
                $i++;
              }
            }
            return $imagesCode;
          } else {
            $picasaAlbum = $albumId;
            $jsCode .= 'if (!window.sexylightboxEnabled) {SexyLightbox.initialize(' . $dFP . ');} if (!window.sexylightboxEnabled) {window.sexylightboxEnabled = true;}';
            $feedURL = "http://picasaweb.google.com/data/feed/api/user/$picasaUser/album/$picasaAlbum";
            ini_set('allow_url_fopen', 1);
            $sxml = simplexml_load_file($feedURL);
            if (!$sxml) {
              $string= file_get_contents($feedURL);
              $ch = curl_init();
              curl_setopt($ch, CURLOPT_URL, $feedURL);
              curl_setopt($ch, CURLOPT_HEADER, 0);
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
              $content = curl_exec($ch);
              $sxml = simplexml_load_string($content);
            }
            if (!$sxml || !$sxml->children('http://a9.com/-/spec/opensearchrss/1.0/')) {
              $imagesCode = 'Picasa user or album is not correct';
            } else {
              $counts = $sxml->children('http://a9.com/-/spec/opensearchrss/1.0/');
              $total = $counts->totalResults;
              $k = 0;
              $i = 1;
              foreach ($sxml->entry as $entry) {
                $summary = $entry->summary;
                $title = htmlspecialchars($summary, ENT_QUOTES);

                $gphoto = $entry->children('http://schemas.google.com/photos/2007');
                
                $size = $gphoto->size;
                $height = $gphoto->height;
                $width = $gphoto->width;

                $media = $entry->children('http://search.yahoo.com/mrss/');
                
                if ($picasaImageSize && $picasaImageSize == 'tiny') {
                  $thumbnail = $media->group->thumbnail[0];
                } else if ($picasaImageSize && $picasaImageSize == 'small') {
                  $thumbnail = $media->group->thumbnail[1];
                } else if ($picasaImageSize && $picasaImageSize == 'big') {
                  $thumbnail = $media->group->thumbnail[2];
                } else {
                  $thumbnail = $media->group->thumbnail[1];
                }

                
                $pContent = $entry->content;
                
                $tags = $media->group->keywords;
                
                $attr = $pContent->attributes();
                $tattr = $thumbnail->attributes();
                if ($caption) {
                  $imagesCode .= "<div class='asl_image_caption'>";
                }
                $imgSrc = $attr->{'src'};
                $imagesCode .= "<a href='" . substr($imgSrc, 0, strripos ($imgSrc, '/')) . '/s' . $picasaLargeImageSize . substr($imgSrc, strripos ($imgSrc, '/')) . "' rel='sexylightbox[" . $id . "]' class='artsexylightboxpreview' ";
                if ( ($numberOfImages && $numberOfImages > 0) && $i > $numberOfImages) {
                  $imagesCode .= " style='display:none;' ";
                }
                if ($title) {
                  $imagesCode .= "title='" . $title . "'";
                }
                if ($singleImage && $k != 0) {
                  $imagesCode .= " style='display:none;'";
                }
                $imagesCode .= "><img alt='$title' class='artsexylightbox' src='" . $tattr->{'url'} . "'";
                if ($previewHeight) {
                  $imagesCode .= " height='$previewHeight' ";
                }
                if ($previewWidth) {
                  $imagesCode .= "width='$previewWidth' ";
                }
                $imagesCode .= "/></a>";
                if ($caption) {
                  $title = str_replace('&lt;', '<', $title);
                  $title = str_replace('&gt;', '>', $title);
                  $imagesCode .= '<div class="asl_caption">' . html_entity_decode($title) . '</div>';
                  $imagesCode .= '</div>';
                }
                $k++;
                $i++;
              }
            }
            $imagesCode .= '</div>';
            if (isset($popup) && $popup == 'false') {
              return $imagesCode;
            }
            $jsCode .= '})});</script>';
            return $imagesCode.$jsCode;
          }
        }
			}
		/** Show article links **/
		} else if ($photobucket) {
      if ($carousel) {
        $imagesCode .= '<div class="asl_prev" id="aslcarouselprev_' . $id . '"></div><div class="asl_carousel" id="aslcarousel_' . $id . '"><ul>';
        $jsCode .= 'if (!window.sexylightboxEnabled) {SexyLightbox.initialize(' . $dFP . ');} if (!window.sexylightboxEnabled) {window.sexylightboxEnabled = true;}';
        $feedURL = $photobucket;
        ini_set('allow_url_fopen', 1);
        $sxml = simplexml_load_file($feedURL);
        if (!$sxml) {
          $string= file_get_contents($feedURL);
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, $feedURL);
          curl_setopt($ch, CURLOPT_HEADER, 0);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          $content = curl_exec($ch);
          $sxml = simplexml_load_string($content);
        }
        if (!$sxml || !$sxml->channel) {
          $imagesCode = 'Photobucket user or album is not correct';
        } else {
          $k = 0;
          $i = 1;
          foreach ($sxml->channel->item as $entry) {
            if ( ($numberOfImages && $numberOfImages > 0) && $i > $numberOfImages) {
              break;
            }
            $media = $entry->children('http://search.yahoo.com/mrss/');
            $media2 = $media->content->children('http://search.yahoo.com/mrss/');
            $media3 = $media->content->attributes();
            $imgLink = $media3->{'url'};
            
            $attrs = $media2->thumbnail->attributes();

            $title = $entry->title;
            $thumbnail = $attrs->{'url'};
            
            if ($caption) {
              $imagesCode .= "<div class='asl_image_caption'>";
            }
            $imagesCode .= "<li><a href='" . $imgLink . "' rel='sexylightbox[" . $id . "]' class='artsexylightboxpreview' ";
            if ($title) {
              $imagesCode .= "title='" . $title . "'";
            }
            if ($singleImage && $k != 0) {
              $imagesCode .= " style='display:none;'";
            }
            $imagesCode .= "><img alt='$title' class='artsexylightbox' src='" . $thumbnail . "'";
            if ($previewHeight) {
              $imagesCode .= " height='$previewHeight' ";
            }
            if ($previewWidth) {
              $imagesCode .= "width='$previewWidth' ";
            }
            $imagesCode .= "/></a></li>";
            if ($caption) {
              $title = str_replace('&lt;', '<', $title);
              $title = str_replace('&gt;', '>', $title);
              $imagesCode .= '<div class="asl_caption">' . html_entity_decode($title) . '</div>';
              $imagesCode .= '</div>';
            }
            $k++;
            $i++;
          }
        }
        $imagesCode .= '</ul></div><div id="aslcarouselnext_' . $id . '" class="asl_next"></div><div style="clear:both"></div>';
				$jsCode .= 'asljQuery(function() {asljQuery("#aslcarousel_' . $id . '").jCarouselLite({vertical: ' . $vertical . ', circular: false, visible: ' . $carousel_visible . ', scroll: ' . $carousel_scroll . ', btnNext: "#aslcarouselnext_' . $id . '", btnPrev: "#aslcarouselprev_' . $id . '"});});';
        $jsCode .= '})});</script>';
        $imagesCode .= '</div>';
        return $imagesCode.$jsCode;
      } else {
        $jsCode .= 'if (!window.sexylightboxEnabled) {SexyLightbox.initialize(' . $dFP . ');} if (!window.sexylightboxEnabled) {window.sexylightboxEnabled = true;}';
        $feedURL = $photobucket;
        ini_set('allow_url_fopen', 1);
		$feedURL = str_replace(" ", "%20", $feedURL);
        $sxml = simplexml_load_file($feedURL);
        if (!$sxml) {
          $string= file_get_contents($feedURL);
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, $feedURL);
          curl_setopt($ch, CURLOPT_HEADER, 0);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          $content = curl_exec($ch);
          $sxml = simplexml_load_string($content);
        }
        if (!$sxml || !$sxml->channel) {
          $imagesCode = 'Photobucket user or album is not correct';
        } else {
          $k = 0;
          $i = 1;
          foreach ($sxml->channel->item as $entry) {
            if ( ($numberOfImages && $numberOfImages > 0) && $i > $numberOfImages) {
              break;
            }
            $media = $entry->children('http://search.yahoo.com/mrss/');
            $media2 = $media->content->children('http://search.yahoo.com/mrss/');
            $media3 = $media->content->attributes();
            $imgLink = $media3->{'url'};
            
            $attrs = $media2->thumbnail->attributes();

            $title = $entry->title;
            $thumbnail = $attrs->{'url'};
            
            if ($caption) {
              $imagesCode .= "<div class='asl_image_caption'>";
            }
            $imagesCode .= "<a href='" . $imgLink . "' rel='sexylightbox[" . $id . "]' class='artsexylightboxpreview' ";
            if ($title) {
              $imagesCode .= "title='" . $title . "'";
            }
            if ($singleImage && $k != 0) {
              $imagesCode .= " style='display:none;'";
            }
            $imagesCode .= "><img alt='$title' class='artsexylightbox' src='" . $thumbnail . "'";
            if ($previewHeight) {
              $imagesCode .= " height='$previewHeight' ";
            }
            if ($previewWidth) {
              $imagesCode .= "width='$previewWidth' ";
            }
            $imagesCode .= "/></a>";
            if ($caption) {
              $title = str_replace('&lt;', '<', $title);
              $title = str_replace('&gt;', '>', $title);
              $imagesCode .= '<div class="asl_caption">' . html_entity_decode($title) . '</div>';
              $imagesCode .= '</div>';
            }
            $k++;
            $i++;
          }
        }
        $imagesCode .= '</div>';
        if (isset($popup) && $popup == 'false') {
          return $imagesCode;
        }
        $jsCode .= '})});</script>';
        return $imagesCode.$jsCode;
      }
		} else if ($section || $category) {
			$section = intval($section);
			$category = intval($category);
			$jsCode .= 'if (!window.sexylightboxEnabled) {SexyLightbox.initialize(' . $dFP . ');} if (!window.sexylightboxEnabled) {window.sexylightboxEnabled = true;}';
			$db	=& JFactory::getDBO();
			$query = 'SELECT * FROM #__content ';
			if (isset($section) && $section && isset($category) && $category) {
				$query .= 'WHERE sectionid = ' . $section;
				$query .= ' AND catid = ' . $category;
			} else {
				if (isset($section) && $section) {
					$query .= 'WHERE sectionid = ' . $section;
				}
				if (isset($category) && $category) {
					$query .= 'WHERE catid = ' . $category;
				}
			}
      if ($sortBy) {
        $query .= ' ORDER BY ' . $sortBy . ' ' . $sort;
      }
			
			$db->setQuery($query);
			$rows = $db->loadObjectList();
			if (!$previewWidth) {
				$previewWidth = '600';
			}
			if (!$previewHeight) {
				$previewHeight = '400';
			}
			foreach ($rows as $row) {
				$title = $row->title;
				$uri = JFactory::getURI();
				if ($articleContentOnly) {
					$url = $uri->root().'index.php?option=com_content&view=article&tmpl=component&id='.$row->id;
				} else {
					$url = $uri->root().'index.php?option=com_content&view=article&id='.$row->id;
				}
				if (isset($category) && $category) {
					$url .= '&catid=' . $category;
				}
				$url .= '&TB_iframe=true&';
				$url .= 'width=' . $previewWidth;
				$url .= '&height=' . $previewHeight;
				$imagesCode .= "<a href='$url' rel='sexylightboxgallery[" . $id . "]' class='artsexylightboxlink' title='$title'>";
				if ($singleImage) {
				  $imagesCode .= '<img src="' . JURI::root() . $singleImage . '" alt="article image" />';
				} else {
					if ($showHits) {
						$imagesCode .= $title . ' (' . $row->hits . ')';
					} else {
						$imagesCode .= $title;
					}
				}
				$imagesCode .= "</a>";
			}
			$jsCode .= '})});</script>';
			$imagesCode .= "</div>";
			return $imagesCode.$jsCode;
		/** Show inline content **/
		} else if (isset($contentId)) {
			$id = $contentId;
			$url = "#TB_inline?inlineId=$id";
			if (isset($height) && !empty($height)) $url .= "&height=$height";
			if (isset($width) && !empty($width)) $url .= "&width=$width";
			if (isset($modal) && !empty($modal)) $url .= "&modal=$modal";
			if ($singleImage) {
				$text = "<img alt='$pAlt' class='artsexylightbox_singleimage' src='". $singleImage ."'";
				if ($previewHeight) {
					$text .= " height='$previewHeight' ";
				}
				if ($previewWidth) {
					$text .= "width='$previewWidth' ";
				}
				$text .= "/>";
			} else if ($singleText) {
				$text = $singleText;
			}
			$inlineCode = "<a class='artsexylightboxpreview' href='$url' rel='sexylightbox' title='$title'>$text</a>";
			$jsCode .= 'SexyLightbox.initialize(' . $dFP . ');';
			if ($hideContent) {
				$jsCode .= 'document.getElementById("' . $id . '").style.display = "none";';
			}
			$jsCode .= 'if (!window.sexylightboxEnabled) {window.sexylightboxEnabled = true;}';
      if ($autoLoad) {
				$jsCode .= 'SexyLightbox.hook("a.artsexylightboxpreview");';
			}
			$jsCode .= '})});</script>';
			return $jsCode.$inlineCode;
    } else if (isset($content) && !empty($content)) {
			$id = uniqid('artinline_', false);
			$url = "#TB_inline?inlineId=$id";
			if (isset($height) && !empty($height)) $url .= "&height=$height";
			if (isset($width) && !empty($width)) $url .= "&width=$width";
			if (isset($modal) && !empty($modal)) $url .= "&modal=$modal";
			if ($singleImage) {
				$text = "<img alt='$pAlt' class='artsexylightbox_singleimage' src='". $singleImage ."'";
				if ($previewHeight) {
					$text .= " height='$previewHeight' ";
				}
				if ($previewWidth) {
					$text .= "width='$previewWidth' ";
				}
				$text .= "/>";
			} else if ($singleText) {
				$text = $singleText;
			}
			$inlineCode = "<span id='$id'>$content</span><a class='artsexylightboxpreview' href='$url' rel='sexylightbox' title='$title'>$text</a>";
			$jsCode .= 'SexyLightbox.initialize(' . $dFP . ');';
			if ($hideContent) {
				$jsCode .= 'document.getElementById("' . $id . '").style.display = "none";';
			}
			$jsCode .= 'if (!window.sexylightboxEnabled) {window.sexylightboxEnabled = true;}';
                        if ($autoLoad) {
				$jsCode .= 'SexyLightbox.hook("a.artsexylightboxpreview");';
			}
			$jsCode .= '})});</script>';
			return $jsCode.$inlineCode;
		} else {
			$jsCode .= 'if (!window.sexylightboxEnabled) {SexyLightbox.initialize(' . $dFP . ')};if (!window.sexylightboxEnabled) {window.sexylightboxEnabled = true;}';
      if ($autoLoad) {
				$jsCode .= 'SexyLightbox.hook("a[rel^=sexylightbox]");';
			} else if ($articleAutoLoad) {
        $db	=& JFactory::getDBO();
        $query = 'SELECT * FROM #__content where id = ' . mysql_real_escape_string($articleAutoLoad);
        $db->setQuery($query);
        $row = $db->loadObject();
        if ($row && $row->state) {
          $testCode = '<a style="display:none" rel="sexylightbox" href="' . JURI::root() . 'index.php?option=com_content&view=article&id=' . $articleAutoLoad . '&tmpl=component&TB_iframe=true&amp;height=' . $height . '&amp;width=' . $width . '">link</a>';
          $jsCode .= 'SexyLightbox.hook("a[rel^=sexylightbox]");';
        }
      }
			$jsCode .= '})});</script>';
			return $testCode . $jsCode;
		}
	}
	
	/** Get parameters from back-end settings **/
	function getPluginParams($params, $nativeParams = null) {
		if ($nativeParams == null) {
			$nativeParams = $params;
		}
		$pluginParams = $params;
		if (!empty ($nativeParams)) {
			foreach($nativeParams as $key => $value) {
				$pValue = @$nativeParams->$key;
				if (isset ($pValue) && !empty($pValue)) {
					$pluginParams[$key] = $nativeParams->$key;
				}
			}
		}
		
		return $pluginParams;
	}
	
	/** Get parameters from user input **/
	function getClientParams($params, $finalParams) {
		foreach($params as $key => $value) {
			if(@$params[$key]) $finalParams[$key] = htmlspecialchars($params[$key], ENT_QUOTES);
		}
		return $finalParams;
	}
	
	/** Check whether file is an image **/
	function isImage($fileName) {
		$extensions = array('.jpeg', '.jpg', '.gif', '.png', '.bmp', '.tiff', '.tif', '.ico', '.rle', '.dib', '.pct', '.pict');
		$extension = substr($fileName, strrpos($fileName,"."));
		if (in_array(strtolower($extension), $extensions)) return true;
		return false;
	}
	
	function isFVL($fileName) {
		$extension = substr($fileName, strrpos($fileName,"."));
		if ($extension = '.flv') return true;
		return false;
	}
	
	/** Check whether Asido extension is loaded **/
	function isExtensionsLoaded($drivers = array('imagick', 'gd', 'magickwand')) {
		reset($drivers);

		foreach ($drivers as $driver) {
			if ($driver == 'imagick' && !function_exists('imagick_readImage')) continue;
			
			if (@extension_loaded($driver)) {
				return true;
			}
		}
		
		return false;
	}
	
	/** Load Asido extensions **/
	function loadExtensions($drivers = array('imagick', 'gd', 'magickwand')) {
		$driverAliases = array('imagick' => 'imagick_ext', 'magickwand' => 'magick_wand');
		reset($drivers);
		foreach ($drivers as $driver) {
			if (@extension_loaded($driver)) {
				if ($driver == 'imagick' && !function_exists('imagick_readImage')) continue ;
				
				if (array_key_exists($driver, $driverAliases)) $driver = $driverAliases[$driver];
				asido::driver($driver);
				return true;
			}
		}

		return false;
	}

}

/** Sort images in ascending order **/
function artSLFileAscSort($a, $b) {
	list ($anum, $aalph) = explode ('.', $a);
	list ($bnum, $balph) = explode ('.', $b);
	
	if ($anum == $bnum) return strcmp($aalph, $balph);
	return $anum < $bnum ? -1 : 1;
}

/** Sort files in descending order **/
function artSLFileDescSort($a, $b) {
	list ($anum, $aalph) = explode ('.', $a);
	list ($bnum, $balph) = explode ('.', $b);
	
	if ($anum == $bnum) return !strcmp($aalph, $balph);
	return $anum > $bnum ? -1 : 1;
}

?>